Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Resolve Android 'Screen Overlay Detected' issue
Message of Issue: "Screen overlay detected - To change this permission setting you first have to turn off the screen overlay from Settings > Apps"
Scope of issue: This only applies to Android M (6.0/API v23) Or Over
Explanation: Other apps installed on the users device may be utilizing a screen overlay on your phone (e.g. Twilight, Red Moon, etc...)
Screen overlays are virtual layers that cover part or all of screen while another app is in the foreground.
It may be dangerous for android to allow you to to change a sensitive setting while an overlay is active because you may prone to 'tap-jacking'
(i.e. a malicious application displays a fake user interface that seems like it can be interacted with, but actually passes interaction events such as finger taps to a hidden user interface behind it.).
Therefore to improve security, android doesn't allow you to change sensitive settings while an active overlay is detected, unless the user permits the app to do so.
Source: https://commonsware.com/blog/2016/03/24/system-alert-window-now-more-hidden-than-ever.html
Misconception(s):
Other apps cause this issue - The other apps may instigate it if the code is not optimised as shown below. (I currently tested it while Red Moon was active)
Further Info: http://www.androidpolice.com/2015/09/07/android-m-begins-locking-down-floating-apps-requires-users-to-grant-special-permission-to-draw-on-other-apps/
Note: The scope of the permission may be only within that Activity
______________________________________
________| |_______
\ | Here we go | /
\ | | /
/ |______________________________________| \
/__________) (_________\
1. -------------ADD 'SYSTEM_ALERT_WINDOW' permission on Manifest.xml
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
2. ------------- IMPLEMENT METHOD BELOW, to run this check at the onCreate of the first Activity
public final static int PERM_REQUEST_CODE_DRAW_OVERLAYS = 1234;
/**
* Permission to draw Overlays/On Other Apps, related to 'android.permission.SYSTEM_ALERT_WINDOW' in Manifest
* Resolves issue of popup in Android M and above "Screen overlay detected- To change this permission setting you first have to turn off the screen overlay from Settings > Apps"
* If app has not been granted permission to draw on the screen, create an Intent &
* set its destination to Settings.ACTION_MANAGE_OVERLAY_PERMISSION &
* add a URI in the form of "package:<package name>" to send users directly to your app's page.
* Note: Alternative Ignore URI to send user to the full list of apps.
*/
public void permissionToDrawOverlays() {
if (android.os.Build.VERSION.SDK_INT >= 23) { //Android M Or Over
if (!Settings.canDrawOverlays(this)) {
Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, Uri.parse("package:" + getPackageName()));
startActivityForResult(intent, PERM_REQUEST_CODE_DRAW_OVERLAYS);
}
}
}
3. ------------- Called on the activity, to check on the results returned of the user action within the settings
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == PERM_REQUEST_CODE_DRAW_OVERLAYS) {
if (android.os.Build.VERSION.SDK_INT >= 23) { //Android M Or Over
if (!Settings.canDrawOverlays(this)) {
// ADD UI FOR USER TO KNOW THAT UI for SYSTEM_ALERT_WINDOW permission was not granted earlier...
}
}
}
}
@CodeHore

This comment has been minimized.

Copy link

@CodeHore CodeHore commented Oct 21, 2016

Help me understand what PERM_REQUEST_CODE_DRAW_OVERLAYS is set to in the code?
I seem to be missing something here

@Aeonitis

This comment has been minimized.

Copy link
Owner Author

@Aeonitis Aeonitis commented Nov 15, 2016

Hey CodeH*re, I am sorry I was late to reply, I've just updated it now. It's really just a integer constant. Hope you had moved on by now. Apologies...

@clucasalcantara

This comment has been minimized.

Copy link

@clucasalcantara clucasalcantara commented Feb 15, 2017

Ok, I've created the permissionToDrawOverlays method, but, When/Where I've to call it?

@kaisar945

This comment has been minimized.

Copy link

@kaisar945 kaisar945 commented Mar 10, 2017

Can i bypass Screen Overlay Detected?

@kilicpl

This comment has been minimized.

Copy link

@kilicpl kilicpl commented Jul 26, 2018

hi , thx a lot for the great code but is the code still apply? It gives red error on "canDrawOverlays" and "ACTION_MANAGE_OVERLAY_PERMISSION".. I just copied the method inside the main activity then called in oncreate, what did i miss do you think?
Thanks a lot again

@remylavergne

This comment has been minimized.

Copy link

@remylavergne remylavergne commented Oct 23, 2019

hi , thx a lot for the great code but is the code still apply? It gives red error on "canDrawOverlays" and "ACTION_MANAGE_OVERLAY_PERMISSION".. I just copied the method inside the main activity then called in oncreate, what did i miss do you think?
Thanks a lot again

Did you use the package "package android.provider;" for Settings.canDrawOverlays(this) ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment