Skip to content

Instantly share code, notes, and snippets.

@Aeonitis
Last active October 28, 2023 06:05
Show Gist options
  • Star 18 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save Aeonitis/2337b1ca652173839395be82db7d05c3 to your computer and use it in GitHub Desktop.
Save Aeonitis/2337b1ca652173839395be82db7d05c3 to your computer and use it in GitHub Desktop.
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
Copy link

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

@Aeonitis
Copy link
Author

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 an arbitrary integer constant. Hope you had moved on by now. Apologies...

@clucasalcantara
Copy link

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

@kaisar945
Copy link

Can i bypass Screen Overlay Detected?

@kilicpl
Copy link

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
Copy link

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) ?

@Aeonitis
Copy link
Author

Aeonitis commented Oct 26, 2023

@remylavergne I keep missing notifications for this. I don't have my own code around anymore, but I see someone else which might be using this solution. Someone else who seems to do so is here.

Again, sorry for the delay. I wonder if after 7 years since I posted this, if it has already been resolved :(

@remylavergne
Copy link

Yes, since the problem is solved and the app is in production 😂
Thanks !

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