How to check "Displaying popup windows while running in the background" permission?

337 Views Asked by At

I'm coding an android app that needs to start an activity in the background.

On XiaoMi device, my app must be accepted "Displaying popup windows while running in the background" permission to do that.

However, I don't know when this permission is granted to guide users to accept it. Is there any solution to check this permission is granted or not?

Thanks

enter image description here

1

There are 1 best solutions below

0
On

Reposting my previously deleted answer once again

Here is a working way to find out if it's granted (tested on MIUI 14.0.3)

public static final int OP_BACKGROUND_START_ACTIVITY = 10021;

@SuppressWarnings("JavaReflectionMemberAccess")
@TargetApi(19)
public static boolean isBackgroundStartActivityPermissionGranted(Context context) {
    try {
        AppOpsManager mgr = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE);
        Method m = AppOpsManager.class.getMethod("checkOpNoThrow", int.class, int.class, String.class);
        int result = (int) m.invoke(mgr, OP_BACKGROUND_START_ACTIVITY, android.os.Process.myUid(), context.getPackageName());
        return result == AppOpsManager.MODE_ALLOWED;
    } catch (Exception e) {
        Log.d("Exception", e.toString());
    }
    return true;
}

You can find an even more detailed code here: https://github.com/zoontek/react-native-permissions/issues/412#issuecomment-1590376224