AndroidManifest.xml 에 있는 퍼미션 관련부분을 아래 코드에 적용하면 됩니다.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
checkGranted();
}
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
public void checkGranted() {
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (PackageManager.PERMISSION_GRANTED != checkSelfPermission(Manifest.permission.INTERNET) ||
PackageManager.PERMISSION_GRANTED != checkSelfPermission(Manifest.permission.ACCESS_NETWORK_STATE) ||
PackageManager.PERMISSION_GRANTED != checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) ||
PackageManager.PERMISSION_GRANTED != checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE) ||
PackageManager.PERMISSION_GRANTED != checkSelfPermission(Manifest.permission.READ_PHONE_STATE) ||
PackageManager.PERMISSION_GRANTED != checkSelfPermission(Manifest.permission.VIBRATE) ||
PackageManager.PERMISSION_GRANTED != checkSelfPermission(Manifest.permission.CAMERA)
) {
requestPermissions(new String[]{
Manifest.permission.INTERNET,
Manifest.permission.ACCESS_NETWORK_STATE,
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.READ_PHONE_STATE,
Manifest.permission.VIBRATE,
Manifest.permission.CAMERA},1);
} else {
}
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == 1) {
if (grantResults.length > 0) {
for (int i=0; i<grantResults.length; ++i) {
if (grantResults[i] == PackageManager.PERMISSION_DENIED) {
new AlertDialog.Builder(this).setTitle("알림").setMessage("권한을 허용하셔야 앱을 이용할 수 있습니다.")
.setPositiveButton("종료", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
finish();
}
}).setNegativeButton("권한 설정", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
checkGranted();
}
}).setCancelable(false).show();
return;
}
}
}
}
}