How to programmatically grant the "draw over other apps" permission in android?
Asked Answered
A

4

28

How can I programmatically grant the permission in Settings -> Apps -> Draw over other apps in Android? I want to use system alert window but unable to in Android Marshmallow without forcing the user to go through the Settings app and grant the permission first.

Amoy answered 1/11, 2016 at 6:52 Comment(4)
What's your problem please specify.Duwalt
dear sir when use system.alert.window permission then one granted window open for on the permission. i want to do it programatically. I do not want to open that windowAmoy
Please post your code. What have you tried so far?Perpetuity
This is a good question. People keep flagging down great useful questions for no reason at all. Check my answer, found it on stackoverflow too and it worked for me.Coucher
D
45

You can check and ask for overlay permission to draw over other apps using this

if (!Settings.canDrawOverlays(this)) {
    Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, Uri.parse("package:" + getPackageName()));
    startActivityForResult(intent, 0);
}
Decosta answered 1/11, 2016 at 7:2 Comment(7)
sir this code generate a system window for on or of the overlay permissionAmoy
I want it programmatically on no need to user controlAmoy
This code checks if your app has overlay permission. If it doesn't it redirects the user to Settings>Apps>draw over apps page to let the user grant permission. There is no way you can programatically grant yourself permission from Android Marasmellow.Decosta
I wouldn't be so sure about saying there is way to do this. There is an app called XOutOf10 that enables this permission without user interaction.Macerate
If you checkout the description in their play store it says "Android 6.0+ Users: In your first run, please make sure you "permit drawing over apps"." which the user manually has to enable. It was possible before marshmallow, but no way afterDecosta
"Every app that requests the SYSTEM_ALERT_WINDOW permission and that is installed through the Play Store (version 6.0.5 or higher is required), will have granted the permission automatically."Coucher
@CuriousMind No, it didn't happen for me. I have tried uploading to playstore with <uses-permission ... SYSTEM_ALERT_WINDOW > and <permission ... SYSTEM_ALERT_WINDOW > with protectionLevel but still the user has to enable it manually. Idk how facebook messenger is doing it by defaultResistant
W
21
if (!Settings.canDrawOverlays(this)) {
    Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, 
    Uri.parse("package:" + getPackageName()));
    startActivityForResult(intent, 0);
}
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
Wonacott answered 28/5, 2019 at 6:8 Comment(1)
Please add some explanation to avoid to come this post in low quality posts.Kindle
S
11

Here's the code for automatic granting the SYSTEM_ALERT_WINDOW permission to the package. To run this code, your Android application must be system (signed by platform keys).

This method is based on the following Android source code files: AppOpsManager.java and DrawOverlayDetails.java, see the method DrawOverlayDetails.setCanDrawOverlay(boolean newState).

@TargetApi(Build.VERSION_CODES.KITKAT)
public static void autoSetOverlayPermission(Context context, String packageName) {
    PackageManager packageManager = context.getPackageManager();
    int uid = 0;
    try {
        ApplicationInfo applicationInfo = packageManager.getApplicationInfo(packageName, 0);
        uid = applicationInfo.uid;
    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
        return;
    }

    AppOpsManager appOpsManager = (AppOpsManager)context.getSystemService(Context.APP_OPS_SERVICE);
    final int OP_SYSTEM_ALERT_WINDOW = 24;
    try {
        Class clazz = AppOpsManager.class;
        Method method = clazz.getDeclaredMethod("setMode", int.class, int.class, String.class, int.class);
        method.invoke(appOpsManager, OP_SYSTEM_ALERT_WINDOW, uid, packageName, AppOpsManager.MODE_ALLOWED);
        Log.d(Const.LOG_TAG, "Overlay permission granted to " + packageName);
    } catch (Exception e) {
        Log.e(Const.LOG_TAG, Log.getStackTraceString(e));
    }
}

}

The code has been tested in Headwind MDM project, it successfully grants "Draw over other apps" permission without any user consent to the Headwind Remote application (disclaimer: I'm the project owner of Headwind MDM and Headwind Remote), when Headwind MDM application is signed by platform keys. The code has been tested on Android 10 (LineageOS 17).

Shinn answered 30/7, 2021 at 9:33 Comment(4)
Hi. I have tried your code but I have this exception: Caused by: java.lang.SecurityException: uid 10182 does not have android.permission.UPDATE_APP_OPS_STATS. My app is DeviceOwer... it is not enough?Breadthways
Unfortunately Device Owner is not enough to run this code. You need to sign your app by platform keys and use shared user android.uid.system.Shinn
Can an app signed by platform keys use this code to grant others app permission? (Not itself).Inaccessible
I think this code can also be used to grant the permission to other apps (not tested though). Just replace the packageName with the package ID of the app requiring the permission.Shinn
C
1

Check this question and the answer:

SYSTEM_ALERT_WINDOW - How to get this permission automatically on Android 6.0 and targetSdkVersion 23

"Every app that requests the SYSTEM_ALERT_WINDOW permission and that is installed through the Play Store (version 6.0.5 or higher is required), will have granted the permission automatically."

Coucher answered 16/11, 2018 at 12:51 Comment(3)
No, it didn't happen for me. I have tried uploading to playstore with <uses-permission ... SYSTEM_ALERT_WINDOW > and <permission ... SYSTEM_ALERT_WINDOW > with protectionLevel but still the user has to enable it manually. Idk how facebook messenger is doing it by defaultResistant
And you are installing it through the play store?Coucher
Yes, please have a look on this answer: https://mcmap.net/q/210619/-system_alert_window-how-to-get-this-permission-automatically-on-android-6-0-and-targetsdkversion-23Resistant

© 2022 - 2024 — McMap. All rights reserved.