[Android]SYSTEM_ALAER_WINDOW在Android6.0后无法授权 @ 8/6/2020

技术类
Q: 使用悬浮窗,会弹出“显示在其他应用的上层”权限管理页面是怎么回事?

A:在android6.0之后,android系统觉得开发者要使用SYSTEM_ALERT_WINDOW功能,不只是Manifest宣告一下就可以了,还要在APP执行时再问一次使用者并且解释原因。

官方文档:
As of API level 23, an app cannot draw on top of other apps unless it declares the Manifest.permission.SYSTEM_ALERT_WINDOW permission in its manifest, and the user specifically grants the app this capability. To prompt the user to grant this approval, the app must send an intent with the action ACTION_MANAGE_OVERLAY_PERMISSION, which causes the system to display a permission management screen.


解决办法:使用隐式intent启动ACTION_MANAGE_OVERLAY_PERMISSION,其中携带着应用的包名信息,这样会跳转到一个悬浮窗权限申请界面。

上代码:
@TargetApi(Build.VERSION_CODES.M)
private void getOverlayPermission() {
    Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION);
    intent.setData(Uri.parse("package:" + getPackageName()));
    startActivityForResult(intent, 1);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == 1){
        if (!Settings.canDrawOverlays(this)){
            ToastUtils.showToast(this, "权限未开放");
        } else {
            ToastUtils.showToast(this, "已授权");
        }
    }
}


另:手动设置的路径是:Settings->Apps->App Setting->Draw over other apps 然后手动打开应用的此权限。
作为开发者,也可以:pm grant com.ifyr.launcher android.permission.SYSTEM_ALERT_WINDOW
发布于 8/6/2020 18:08:04 | 评论:0

看帖要回帖...

categories
archives
links
statistics
  • 网志数:1189
  • 评论数:2015