Last active
October 28, 2023 06:05
-
-
Save Aeonitis/2337b1ca652173839395be82db7d05c3 to your computer and use it in GitHub Desktop.
Resolve Android 'Screen Overlay Detected' issue
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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... | |
} | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Yes, since the problem is solved and the app is in production 😂
Thanks !