I'm afraid the bad news.
As you probably know, this requires SYSTEM_ALERT_WINDOW permission.
Since Android M Google began to block this permission to reduce clutter. In this resolution, it is a little unusual that it requires the user to go to the actual settings screen. The normal Android M permission stream does not work for this . To quote the API:
If the application is intended for API level 23 or higher, the application user must explicitly grant this permission to the application through the permission control screen
You use the "Settings" class to check if you have permission , and if not, you need to explain and direct the user to the appropriate settings screen using the intent :
Intent i = new Intent(); i.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS); i.addCategory(Intent.CATEGORY_DEFAULT); i.setData(Uri.parse("package:" + context.getPackageName())); i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); i.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY); i.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS); context.startActivity(i);
This should only affect 23+ devices, since older devices should get permission automatically, but not rely on SDK_INT checking, instead rely on canDrawOverlays , as there are exceptions for some devices before marshmallows
Temporary
source share