Settings.canDrawOverlays(context) returns false on

2019-03-30 01:49发布

问题:

Manifest:

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

Code activity.onCreate():

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && BuildConfig.DEBUG) {
        if (!Settings.canDrawOverlays(this)) {
            Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
                    Uri.parse("package:" + getPackageName()));
            startActivityForResult(intent, OVERLAY_PERMISSION_REQ_CODE);
        }
    }

Problem: Settings.canDrawOverlays(this) returns false even so the permission has been already granted. Hasn't happened before update Pixel to Oreo.

Android version: 8.0.0

I've tried to update build tools: to 26.0.1 and target and compileSdk to 26. It helped on the first launch :after toggling on it returns true, but next time I launch application it returns false again.

回答1:

The problem was in Android Oreo itself.

After I've installed security patch it disappeared.

Solution: Install latest security patch.



回答2:

So I have encountered this issue and my best guess is that now in android O the setting for canDrawOverlays is written asynchronously. I have an activity which starts this screen for the user and when I come back I see that that method returns false regardless of whether the user toggled it on or off. But if I toggle the setting on and then wait some time and go back it seems to reflect correctly. I'm not sure what google was trying to accomplish with this but it makes coding around being able to know if the setting has been turned on or not very difficult now..

Note: if you start your own activity pausing the Settings activity then then the overlay permission is persisted and will return True if enabled. This is something you can use for composing your workaround UX.