How to enable screen overlay permission by default

孤街浪徒 提交于 2019-11-29 07:32:40
MohanadMohie

What you need to do is to send your package name inside the intent, as mentioned in the documentation.

Input: Optionally, the Intent's data URI can specify the application package name to directly invoke the management GUI specific to the package name. For example "package:com.my.app".

So, do something like this:

if (!Settings.canDrawOverlays(this)) {
    Intent myIntent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION);
    myIntent.setData(Uri.parse("package: " + getPackageName()));
    startActivityForResult(myIntent, 101);
}  
EKN

If you are using device which OS is more than 6.0 , please follow below steps

Go to Setting --> Select Apps ---> again select setting icon in Apps ---> select draw over other apps

Select your application and enable draw over other apps permition

Android "Screen Overlay Detected" message if user is trying to grant a permission when a notification is showing

Add below code in your manifest

<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />

You don't have to enable the screen overlay permission instead before asking for permission, Show your Dialog box explaining the user why you need the permission and requesting them to accept the permission. if user chooses no don't show the permission dialog. else show him the permission dialog. This helps you to avoid the screen overlay permission.

You have to leave your activity in order to get permission to draw your screen above some other activities. That is something you cannot avoid.

In order to do that, you have to visit Settings via ACTION_MANAGE_OVERLAY_PERMISSION intent. Since you would like to have an user-friendly app, at the moment you need to ask for such permission, fire some dialog with explanation why you need that and with option for to accept and decline your requirement. On accept, fire the intent and start activity.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!