检测SDKAPI如果是大于或等于23的时候,则弹出自定义的对话框提示用户是否授权,确定的话跳转到设置,应用里面去打开权限,否则退出程序。
// 判断当前系统版本,6.0以上执行权限处理
if (Build.VERSION.SDK_INT >= 23) {
manageSystemPermissions();
} else {
initView();
}
处理权限的请求方法:
/* 处理系统所需权限 */
@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 = "请允许以下权限的申请以确保应用的正常运行:" + permissionsNeeded.get(0);
for (int i = 1; i < permissionsNeeded.size(); i++)
message = message + ", " + permissionsNeeded.get(i);
message = message + "\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, "权限获取失败,正在退出……", Toast.LENGTH_LONG).show();
WelcomeActivity.this.finish();
break;
default:
break;
}
}
});
return;
}
requestPermissions(permissionsList.toArray(new String[permissionsList.size()]),
REQUEST_CODE_ASK_PERMISSIONS);
} else {
initView();
}
}
检测某一权限是否被授权:
/* 检查权限是否已授权 */
@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);
}
}
请求权限的结果回调,只有需求的权限都被授权时,才能进入程序:
/* 请求权限结果回调 */
@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, "权限获取失败,正在退出……", Toast.LENGTH_LONG).show();
this.finish();
}
break;
default:
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
当第一次进行权限申请时以及在进行授权时选中了“不再提示”并拒绝授权的,弹出对话框:
/* 第一次申请权限或点击不再询问后再次申请权限对话框 */
private void showMessageOKCancel(String message, DialogInterface.OnClickListener okListener) {
new AlertDialog.Builder(WelcomeActivity.this).setMessage(message).setPositiveButton("设置", okListener)
.setNegativeButton("取消", okListener).setCancelable(false).create().show();
}
网友评论