檢測SDKAPI如果是大于或等于23的時候,則彈出自定義的對話框提示用戶是否授權(quán),確定的話跳轉(zhuǎn)到設(shè)置,應(yīng)用里面去打開權(quán)限样眠,否則退出程序。
// 判斷當(dāng)前系統(tǒng)版本翠肘,6.0以上執(zhí)行權(quán)限處理
if (Build.VERSION.SDK_INT >= 23) {
manageSystemPermissions();
} else {
initView();
}
處理權(quán)限的請求方法:
/* 處理系統(tǒng)所需權(quán)限 */
@TargetApi(23)
public void manageSystemPermissions() {
final List<String> permissionsNeeded = new ArrayList<String>();
final List<String> permissionsList = new ArrayList<String>();
checkPermission(permissionsList, permissionsNeeded, Manifest.permission.READ_PHONE_STATE, "電話");
checkPermission(permissionsList, permissionsNeeded, Manifest.permission.ACCESS_COARSE_LOCATION, "定位");
checkPermission(permissionsList, permissionsNeeded, Manifest.permission.CAMERA, "相機");
checkPermission(permissionsList, permissionsNeeded, Manifest.permission.WRITE_EXTERNAL_STORAGE, "存儲");
checkPermission(permissionsList, permissionsNeeded, Manifest.permission.RECORD_AUDIO, "錄音");
if (permissionsList.size() > 0) {
if (permissionsNeeded.size() > 0) {
// Need Rationale
String message = "請允許以下權(quán)限的申請以確保應(yīng)用的正常運行:" + permissionsNeeded.get(0);
for (int i = 1; i < permissionsNeeded.size(); i++)
message = message + ", " + permissionsNeeded.get(i);
message = message + "\n" + "請點擊 '設(shè)置' --> '權(quán)限' 開啟相關(guān)權(quán)限 ";
showMessageOKCancel(message, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case DialogInterface.BUTTON_POSITIVE:
startActivity(new Intent()
.setAction(android.provider.Settings.ACTION_APPLICATION_DETAILS_SETTINGS)
.setData(Uri.fromParts("package", "com.zzsoft.mobile.office.sx", null)));
dialog.dismiss();
WelcomeActivity.this.finish();
break;
case DialogInterface.BUTTON_NEGATIVE:
dialog.dismiss();
Toast.makeText(WelcomeActivity.this, "權(quán)限獲取失敗檐束,正在退出……", Toast.LENGTH_LONG).show();
WelcomeActivity.this.finish();
break;
default:
break;
}
}
});
return;
}
requestPermissions(permissionsList.toArray(new String[permissionsList.size()]),
REQUEST_CODE_ASK_PERMISSIONS);
} else {
initView();
}
}
檢測某一權(quán)限是否被授權(quán):
/* 檢查權(quán)限是否已授權(quán) */
@TargetApi(23)
private void checkPermission(List<String> permissionsList, List<String> permissionsNeeded, String permission,
String message) {
if (checkSelfPermission(permission) != PackageManager.PERMISSION_GRANTED) {
permissionsList.add(permission);
// Check for Rationale Option
if (!shouldShowRequestPermissionRationale(permission))
permissionsNeeded.add(message);
}
}
請求權(quán)限的結(jié)果回調(diào),只有需求的權(quán)限都被授權(quán)時锯茄,才能進入程序:
/* 請求權(quán)限結(jié)果回調(diào) */
@TargetApi(23)
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
switch (requestCode) {
case REQUEST_CODE_ASK_PERMISSIONS:
Map<String, Integer> perms = new HashMap<String, Integer>();
// Initial
perms.put(Manifest.permission.READ_PHONE_STATE, PackageManager.PERMISSION_GRANTED);
perms.put(Manifest.permission.ACCESS_COARSE_LOCATION, PackageManager.PERMISSION_GRANTED);
perms.put(Manifest.permission.CAMERA, PackageManager.PERMISSION_GRANTED);
perms.put(Manifest.permission.WRITE_EXTERNAL_STORAGE, PackageManager.PERMISSION_GRANTED);
perms.put(Manifest.permission.RECORD_AUDIO, PackageManager.PERMISSION_GRANTED);
// Fill with results
for (int i = 0; i < permissions.length; i++)
perms.put(permissions[i], grantResults[i]);
// Check for ACCESS_FINE_LOCATION
if (perms.get(Manifest.permission.READ_PHONE_STATE) == PackageManager.PERMISSION_GRANTED
&& perms.get(Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED
&& perms.get(Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED
&& perms.get(Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED
&& perms.get(Manifest.permission.RECORD_AUDIO) == PackageManager.PERMISSION_GRANTED) {
// All Permissions Granted
initView();
} else {
// Permission Denied
Toast.makeText(WelcomeActivity.this, "權(quán)限獲取失敗厢塘,正在退出……", Toast.LENGTH_LONG).show();
this.finish();
}
break;
default:
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
當(dāng)?shù)谝淮芜M行權(quán)限申請時以及在進行授權(quán)時選中了“不再提示”并拒絕授權(quán)的,彈出對話框:
/* 第一次申請權(quán)限或點擊不再詢問后再次申請權(quán)限對話框 */
private void showMessageOKCancel(String message, DialogInterface.OnClickListener okListener) {
new AlertDialog.Builder(WelcomeActivity.this).setMessage(message).setPositiveButton("設(shè)置", okListener)
.setNegativeButton("取消", okListener).setCancelable(false).create().show();
}