[안드로이드] API 23 앱 권한 설정


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;

}

}

}

}

}

API 버전이 올라가니깐 이래저래 참 귀찮게 만드네요~


반응형
블로그 이미지

영은파더♥

가상서버호스팅 VPS 리눅스 서버관리 윈도우 IT

,