[안드로이드] 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 버전이 올라가니깐 이래저래 참 귀찮게 만드네요~
'안드로이드' 카테고리의 다른 글
[안드로이드] Android 8.1 SDK 27 FCM PUSH 알림 문제 (0) | 2019.03.20 |
---|---|
[안드로이드] FCM PHP 서버 PUSH (0) | 2019.03.08 |
[안드로이드] FileUriExposedException 에러 (0) | 2019.03.04 |
[안드로이드] IllegalArgumentException root 에러 (0) | 2019.03.04 |
[안드로이드] WebView Uncaught TypeError (0) | 2019.02.27 |