Settings.ACTION_MANAGE_OVERLAY_PERMISSION permission is not working in all devices above api level 23
Asked Answered
W

5

8

I am testing my App for action overlay on devices like (Coolpad(Lolipop MRI), Samsung Galaxy grand neo(Kitkat), Redmi(Marshmallow), Lenovo z2 plus(Marshmallow)) to show a dialog over incoming call screen. things seems to work for devices other than lenovo z2 plus().

formally asking the permission directly I was getting exception:

   public void testPermission() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        if (!Settings.canDrawOverlays(this)) {
            Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
            Uri.parse("package:" + getPackageName()));
            startActivityForResult(intent, ACTION_MANAGE_OVERLAY_PERMISSION_REQUEST_CODE);
          }
    }
}

android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.settings.action.MANAGE_OVERLAY_PERMISSION

Now I changed asking permission to :

 public void testPermission() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        if (!Settings.canDrawOverlays(this)) {
          Intent intent = new  Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
          Uri.parse("package:" + getPackageName()));
            if (intent.resolveActivity(getPackageManager()) != null) {
                startActivityForResult(intent, ACTION_MANAGE_OVERLAY_PERMISSION_REQUEST_CODE);
            }
        }
    }
}

but it is still not able to ask for the permission in zuk 2. immediate help is appreciated.

Wooster answered 25/5, 2017 at 11:25 Comment(3)
Did you find a solution to this problem? I am having same issue on Android TVCaviar
@Harish did you find any solution??Lambdacism
Any news on this? I am facing the same on an Android 8.0 Smart TV.Landgrabber
N
12

The ACTIVITY_MANAGE_OVERLAY_PERMISSION is added in API level 23. You need to check for the matching activity is available before sending the intent. It's clearly stated in docs that,

Docs: In some cases, a matching Activity may not exist, so ensure you safeguard against this.

Define the permission in manifest.xml for SYSTEM_ALERT_WINDOW,

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

The Intent

    public static int ACTION_MANAGE_OVERLAY_PERMISSION_REQUEST_CODE= 2323;

    public void checkPermission() {
        if (!Settings.canDrawOverlays(this)) {
            Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
                    Uri.parse("package:" + getPackageName()));
            startActivityForResult(intent, ACTION_MANAGE_OVERLAY_PERMISSION_REQUEST_CODE);
        }
    }

And the Result method, The intent will not return any data to the onActivityResult method. It's best practice to check for overlay permission once again in the onActivityResult method

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == ACTION_MANAGE_OVERLAY_PERMISSION_REQUEST_CODE) {
        if (Settings.canDrawOverlays(this)) {
            // You have permission
        }
    }
}

Note: If the app targets API level 23 or higher, the app user must explicitly grant this permission to the app through a permission management screen. The app requests the user's approval by sending an intent with action Settings.ACTION_MANAGE_OVERLAY_PERMISSION. The app can check whether it has this authorization by calling Settings.canDrawOverlays().

Neisa answered 7/12, 2019 at 4:54 Comment(0)
R
1

Based what was found, one should safeguard against this. How to do this is not clear. It would seem the suggestion is do something such as try{}catch

will be attempting to find a more desirable safeguard as this actually means one is attempting to display a view that requires permissions that can not be granted. :(

https://developer.android.com/reference/android/provider/Settings.html#ACTION_MANAGE_OVERLAY_PERMISSION

This article about Android Wear is related to this question.

https://sterlingudell.wordpress.com/2016/01/29/system_alert_window-on-android-wear/

In some cases, a matching Activity may not exist, so ensure you safeguard against this.

Redding answered 12/4, 2018 at 19:27 Comment(3)
Hi Guys I am having the same issue but on Android TV. Is there a way to get it to work on TV?Caviar
Any way to add this at Android TV ? @Caviar ThnaksMalena
@SKG, did you find a way to make it work on TV? It works for me in the emulator but not on the actual device.Denunciate
A
1
if(Build.VERSION.SDK_INT >= 23 && !Settings.canDrawOverlays(this)) { 
    StringBuilder sb = new StringBuilder();
    sb.append("package:");
    sb.append(getPackageName());
    startActivityForResult(new 
    Intent("android.settings.action.MANAGE_OVERLAY_PERMISSION", 
        Uri.parse(sb.toString())),123);
}
Afterbrain answered 7/12, 2019 at 4:36 Comment(0)
A
0
  1. Just remove this line: yourIntent.setData(Uri.parse("package:" + getPackageName())) and your app will show into the settings for permission.

  2. You can see the code below and its working.

    if (!Settings.canDrawOverlays(this)) { // send user to the device settings val myIntent =
    Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION) startActivity(myIntent) }

Agio answered 18/11, 2023 at 12:39 Comment(0)
E
-2

Try, this

It is working for me

  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (!Settings.canDrawOverlays(this)) {
                Intent localIntent = new Intent("android.settings.action.MANAGE_OVERLAY_PERMISSION");
                localIntent.setData(Uri.parse("package:" + getPackageName()));
                localIntent.setFlags(268435456);
                startActivity(localIntent);
            }
        }
Emilemile answered 25/5, 2017 at 11:29 Comment(3)
This did not worked for me: exception: android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.settings.action.MANAGE_OVERLAY_PERMISSIONWooster
Have you put exactly this one "Intent localIntent = new Intent("android.settings.action.MANAGE_OVERLAY_PERMISSION");"Emilemile
The code you show is basically the same as what was in the question...It is understood this is how one would normally handle this yet it does not answer the question.Redding

© 2022 - 2024 — McMap. All rights reserved.