Android app Screen Overlay Detected
This problem occurs when you are trying to show a Toast when an alert dialog is already been displayed or vice versa(well there are other reason as well but newbies mostly face this problem). Structure your UX flow so that you don't show both of them at the same time.
how to fix screen overlay detected programmatically
Ok,
finally I found the solution, I search all the web and can't find anything useful.
The answer is: When you ask for new permission don't ever do something else like showing toast or.... In my case I restart my app and ask for the next permission I use this code for restart the app.
Good luck.
Screen overlay detected blocks Android permissions
This popup is caused by the manifest.PERMISSION.SYSTEM_ALERT_WINDOW permission declared by the manifest.
The are 3 categories of permissions, that developer must be aware of.:
- Normal permission - do nothing with them, just declare in the Manifest
- Vulnerable permissions - declare in Manifest and ask for permission at first time. They can be changed through system settings
- Above dangerous permissions: SYSTEM_ALERT_WINDOW and WRITE_SETTINGS belong to this category. They must be granted, but are not visible in system settings. To request for it you don't use a standard way (
int checkSelfPermission (String permission)
) but you have to checkSettings.canDrawOverlays()
orSettings.System.canWrite()
appropriately
If you don't have SYSTEM_ALERT_WINDOW permission:
- check if you have a
Toast
visible when interacting with the permissions popup. Though the Overlay Detected popup doesn't mention it, aToast
also counts as an overlay - check if any of your dependencies require it.
If you're not sure if you're using this permission, there are a couple of test cases that you can do:
Request this permission by yourself:
public class MainActivity extends AppCompatActivity {
public final static int REQUEST_CODE = 10101;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (checkDrawOverlayPermission()) {
startService(new Intent(this, PowerButtonService.class));
}
}
public boolean checkDrawOverlayPermission() {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
return true;
}
if (!Settings.canDrawOverlays(this)) {
Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
Uri.parse("package:" + getPackageName()));
startActivityForResult(intent, REQUEST_CODE);
return false;
} else {
return true;
}
}
@Override
@TargetApi(Build.VERSION_CODES.M)
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_CODE) {
if (Settings.canDrawOverlays(this)) {
startService(new Intent(this, PowerButtonService.class));
}
}
}
}Check if this or this is not your case
Check out this post to be aware, that when installing app through Play Store this permission is automatically granted (I have not checked is, therefore cannot confirm)
The permission model can be understood, just sometimes requires more digging.
Related Topics
How to Make Drawerlayout to Display Below the Toolbar
How to Save Image in Shared Preference in Android | Shared Preference Issue in Android with Image
Programmatically Add View One Below Other in Relative Layout
Java.Lang.Noclassdeffounderror: Com.Google.Android.Gms.Internal.Zzmp
Android Java.Lang.Illegalstateexception: Couldn't Read Row 0, Col 0 from Cursorwindow
Android App Enable Nfc Only for One Activity
Getting Data from Android Play Store
How to Use Room Persistence Library with Pre-Populated Database
Get Date from Datepicker Using Dialogfragment
Get All Child Views Inside Linearlayout at Once
Event Onclick for a Button in a Custom Notification
Naming My Application in Android
Change Background Color of Android Menu
What's the Maximum Size for an Android Shared Preference Value
How to Load Bufferedimage in Android
Understanding Android: Zygote and Dalvikvm
How to Align Android Toolbar Menu/Icons to the Left Like in Google Maps App