I'm developing the MDM app for parents to control children's devices and it uses permission SYSTEM_ALERT_WINDOW
to display warnings on device if forbidden action has performed.
On devices with SDK 23+ (Android 6.0) during installation the app checks the permission using this method:
Settings.canDrawOverlays(getApplicationContext())
and if this method returns 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);
But on device with SDK 26 (Android 8.0), when user has successfully granted permission and returned to the app by pressing back button, method canDrawOverlays()
still returns false
, until user doesn't close the app and starts it again or just chooses it in the recent apps dialog. I tested it on latest version of virtual device with Android 8 in Android Studio because I didn't have real device.
I've done 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 restart 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?