从清单权限不会在Android中工作6会在、清单、权限、工作

2023-09-12 00:11:50 作者:习惯了一个人安静的想你、

它完全忽略了:

  <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>

所以我就异常:

So I got exception:

Caused by: android.view.WindowManager$BadTokenException: Unable to add window android.view.ViewRootImpl$W@86fb55b -- permission denied for this window type

它甚至没有列出的:

It's not even listed:

我应该如何解决?谢谢你。

How should I fix it? Thanks.

编辑:

它列在配置应用程序/高级/笼络其他应用程序。所以,我打开它,现在它工作正常,但为什么没有任何对话框询问许可,当我跑我的应用程序?所有perrmissions是关闭的deafult,我需要去设置和mannualy打开它?

It's listed in Configure apps/ Advanced / Draw over other app. So i turn it on and now it works fine, but why there isn't any dialog to ask about permission when i run my app? All perrmissions was turned off by deafult and i need to go to settings and mannualy turn it on?

推荐答案

感谢的 CommonsWare的博客文章,我得到了一些线索。

Thanks to CommonsWare's blog post, I got some clue.

假设你的code是在活动片段,检查覆盖权限,并提出要求如果需要的话:

Assuming your code is in Activity or Fragment, check the overlay permission and make a request for it if necessary:

public static int OVERLAY_PERMISSION_REQ_CODE = 1234;

public void someMethod() {
    if (!Settings.canDrawOverlays(this)) {
        Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
                Uri.parse("package:" + getPackageName()));
        startActivityForResult(intent, OVERLAY_PERMISSION_REQ_CODE);
    }
}

然后,重新检查的权限为更好的UX:

Then, re-check the permission for better UX:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == OVERLAY_PERMISSION_REQ_CODE) {
        if (!Settings.canDrawOverlays(this)) {
            // SYSTEM_ALERT_WINDOW permission not granted...
        }
    }
}