Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 482
Harish Chauhan Avatar asked May 25 '17 11:05

Harish Chauhan


1 Answers

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().

like image 137
Kiran Maniya Avatar answered Nov 24 '22 00:11

Kiran Maniya