问题:在Android6.0申请SYSTEM_ALERT_WINDOW权限,没有弹授权框?
解决:在android6.0应用开发时,动态申请权限时。按照官方文档无法申请到SYSTEM_ALERT_WINDOW权限,爬stackoverflow找到解决方案。
这个好像是一个bug,可以使用层叠权限解决【Settings.ACTION_MANAGE_OVERLAY_PERMISSION】。
解释博客
直接上代码:
private void checkPermissions(){
Log.d("MainActivity->","SDK_INT->"+Build.VERSION.SDK_INT);
if(Build.VERSION.SDK_INT< 23){
return ;
}
/*
检查是否有悬浮窗的权限
*/
if (!Settings.canDrawOverlays(this)) {
isHasAlertPermission = false;
Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
Uri.parse("package:" + getPackageName()));
startActivityForResult(intent, 101);
}
/*
无效的方法,可以申请读取联系人的权限
*/
int hasPermission = checkSelfPermission(Manifest.permission.READ_CONTACTS);
if(PackageManager.PERMISSION_GRANTED !=hasPermission){
Log.d("MainActivity->","get permissions"+Build.VERSION.SDK_INT);
requestPermissions(new String[]{Manifest.permission.SYSTEM_ALERT_WINDOW},100);
}
}
/*
*申请联系人等权限回调
*/
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
switch (requestCode){
case 100:
if(PackageManager.PERMISSION_GRANTED == grantResults[0]){
isHasAlertPermission = true;
}else {
Toast.makeText(MainActivity.this,"没有获取权限",Toast.LENGTH_LONG).show();
}
break;
default:
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
/**
跳转到设置可以悬浮的权限
**/
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode){
case 101:
if(Build.VERSION.SDK_INT >22){
if (!Settings.canDrawOverlays(this)){
isHasAlertPermission = false;
}
else {
isHasAlertPermission =true;
}
}
break;
default:
super.onActivityResult(requestCode, resultCode, data);
}
}