I have the MDM app for parents to control child's devices and it uses permission SYSTEM_ALERT_WINDOW
to display warnings on child's device when forbidden action performed.
On devices M+ during installation the app checks the permission using this method:
Settings.canDrawOverlays(getApplicationContext())
and if this method return false
the app opens system dialog where user can grant the permission:
Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
Uri.parse("package:" + getPackageName()));
startActivityForResult(intent, REQUEST_CODE);
In Android O, when user successfully grant permission and return to the app by pressing back button, method canDrawOverlays()
still return false
, until user don't close the app and open it again or just choose it in recent apps dialog. I tested it on latest version of virtual device with Android O in Android Studio because I haven't real device.
I did a little research and additionally check the permission with AppOpsManager:
AppOpsManager appOpsMgr = (AppOpsManager) getSystemService(Context.APP_OPS_SERVICE);
int mode = appOpsMgr.checkOpNoThrow("android:system_alert_window", android.os.Process.myUid(), getPackageName());
Log.d(TAG, "android:system_alert_window: mode=" + mode);
And so:
- when the application does not have this permission, the mode is "2"
(MODE_ERRORED) (
canDrawOverlays()
returnsfalse
) when the user - granted permission and returned to the application, the mode is "1"
(MODE_IGNORED) (
canDrawOverlays()
returnsfalse
) - and if you now reopen the app, the mode is "0" (MODE_ALLOWED) (
canDrawOverlays()
returnstrue
)
Please, can anyone explain this behavior to me? Can I rely on mode == 1
of operation "android:system_alert_window"
and assume that the user has granted permission?
Here is my all in one solution, this is a combination of others but serves well for most situations
First checks using the standard check according to Android Docs
Second Check is Using the AppOpsManager
Third and final check if all else fails is to try to display an overlay, if that fails it definitely aint gonna work ;)
Actually on Android 8.0 it returns
true
but there is catch, it returns only when you wait for 5 to 15 seconds and again query for overlay permission usingSettings.canDrawOverlays(context)
method.So what you need to do is show a
ProgressDialog
to user with a message explaining about issue and run aCountDownTimer
to check for overlay permission usingSettings.canDrawOverlays(context)
inonTick
method.Please have a look a sample code from a Fragment:
In my case I was targeting API level < Oreo and the invisible overlay method in Ch4t4's answer does not work as it does not throw an exception.
Elaborating on l0v3's answer above, and the need to guard against the user toggling the permission more than once, I used the below code (Android version checks omitted) :
In the activity / fragment :
To request the permission in the activity / fragment:
And inside
onActivityResult
To guard against your activity being destroyed in the background, inside
onCreate
:and inside
onDestroy
,stopWatchingMode
should be invoked ifonOpChangedListener
is not null, similar toonActivityResult
above.It is important to note that, as of the current implementation (Android O), the system will not de-duplicate registered listeners before calling back. Registering
startWatchingMode(ops, packageName, listener)
, will result in the listener being called for either matching operations OR matching package name, and in case both of them matches, will be called 2 times, so the package name is set to null above to avoid the duplicate call. Also registering the listener multiple times, without unregistering bystopWatchingMode
, will result in the listener being called multiple times - this applies also across Activity destroy-create lifecycles.An alternative to the above is to set a delay of around 1 second before calling
Settings.canDrawOverlays(context)
, but the value of delay depends on device and may not be reliable. (Reference: https://issuetracker.google.com/issues/62047810 )I've found this problems with checkOp too. In my case I have the flow which allows to redirect to settings only when the permission is not set. And the AppOps is set only when redirecting to settings.
Assuming that AppOps callback is called only when something is changed and there is only one switch, which can be changed. That means, if callback is called, user has to grant the permission.
After the app is restored, checking with
canDrawOverlays()
starts worked for me. For sure I restart the app and check if permission is granted via standard way.It's definitely not a perfect solution, but it should work, till we know more about this from Google.
EDIT: I asked google: https://issuetracker.google.com/issues/66072795
EDIT 2: Google fixes this. But it seem that the Android O version will be affected still.
I ran into the same problem. I use a workaround which tries to add an invisible overlay. If an exception is thrown the permission isn't granted. It might not be the best solution, but it works. I can't tell you anything about the AppOps solution, but it looks reliable.