android 特殊权限处理
运行时权限之特殊权限android.permission.SYSTEM_ALERT_WINDOW
- 以下为特殊权限的一种申请写法(android.permission.SYSTEM_ALERT_WINDOW)
在做双屏异显功能时,需要使用到Presentation, 使用Presentation需要android.permission.SYSTEM_ALERT_WINDOW权限, 于是就使用谷歌的权限框架EasyPermission 去处理,申请到权限后,再次打开时,判断还是没有权限,于是抓耳挠腮后,疯狂Google,终于找到了问题所在于是打算记录一下
- android6.0 之前用什么权限,就在manifest中申请就可以了
- android6.0 时增加了运行时权限,将权限分为
1. 普通权限就跟6.0之前一样在manifes中申请就可以,无需特殊处理
2. 危险权限需要开发人员在使用相应功能前,得到功能使用者的授权
3. 特俗权限与危险权限一样,也需要先判断有无授权,然后申请权限,但是写法缺不一样
以下为特殊权限的一种申请写法(android.permission.SYSTEM_ALERT_WINDOW)
private void requestPresentationPermission() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (!Settings.canDrawOverlays(this)) { //这句就是用来判断是否有权限
Xlog.d(TAG, "没有Presentation权限");
presentationDialog();
} else {
Xlog.d(TAG, "有Presentation权限");
startService(new Intent(this, PresentationService.class));
}
} else {
Xlog.d(TAG, "有Presentation权限");
startService(new Intent(this, PresentationService.class));
}
}
private void presentationDialog() {
MaterialAlertDialogBuilder builder = new MaterialAlertDialogBuilder(this);
builder.setTitle("客显权限申请").setMessage(R.string.permission_01).setPositiveButton(R.string.positiveButtonText, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// 这里会跳转到系统权限界面,有用户手动赋予权限
Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, Uri.parse("package:" + getPackageName()));
startActivityForResult(intent, activityResultCallback);
}
}).setNegativeButton(R.string.negativeButtonText, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
ActivityHelper.getInstance().finishAllActivity();
}
}).create().show();
}