Question: I've been getting reports of some Xiaomi devices (e.g. Mi 2, running API level 21) not showing overlays. My app targets API 23.

There are [several](http://forum.xda-developers.com/xiaomi-mi-3/help/link-bubble-playing-nice-t2857607) [posts](http://en.miui.com/thread-30931-1-1.html) out there regarding this. It seems that MIUI devices do not enable this permission at install time (unlike other pre-Marshmallow devices).

Unfortunately, `Settings.canDrawOverlays()` only works on Android 23+.

1. What is the correct way to check whether this permission has not yet been enabled pre-Marshmallow?
2. Is there an Intent to take the user to the relevant MUIU settings page? Maybe: `new Intent("android.settings.action.MANAGE_OVERLAY_PERMISSION", packageName)` but I have no means to test this.

Answer: 
Here is the step by step how to handle this:

At first give below permission in manifest file:

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

```

Or 

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

```

Then handle rest of the things using below code:

```
 public final static int REQUEST_CODE = 65635;

    public void checkDrawOverlayPermission() {
        /** check if we already  have permission to draw over other apps */
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (!Settings.canDrawOverlays(this)) {
                /** if not construct intent to request permission */
                Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
                        Uri.parse("package:" + getPackageName()));
                /** request permission via start activity for result */
                startActivityForResult(intent, REQUEST_CODE);
            }
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode,  Intent data) {
        /** check if received result code
         is equal our requested code for draw permission  */
        if (requestCode == REQUEST_CODE) {
            // ** if so check once again if we have permission */
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                if (Settings.canDrawOverlays(this)) {
                    // continue here - permission was granted
                    goYourActivity();
                }
            }
        }
    }

```

Just call `checkDrawOverlayPermission()` from your LauncherActivity or anywhere as your requirements.

When you execute the project you will see a window and asked for enabling the permission. After allowing permission you will be able to do anything about this.
For searching for while about the issue in Xiaomi, Meizu I have found this. It's working perfectly.. 

```
public static boolean isMiuiFloatWindowOpAllowed(@NonNull Context context) {
    final int version = Build.VERSION.SDK_INT;

    if (version >= 19) {
        return checkOp(context, OP_SYSTEM_ALERT_WINDOW); //See AppOpsManager.OP_SYSTEM_ALERT_WINDOW=24 /*@hide/
    } else {
        return (context.getApplicationInfo().flags & 1<<27) == 1;
    }
}

public static boolean checkOp(Context context, int op, String packageName, int uid) {
    final int version = Build.VERSION.SDK_INT;

    if (version >= 19) {
        AppOpsManager manager = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE);
        try {
            return (AppOpsManager.MODE_ALLOWED == (Integer) ReflectUtils.invokeMethod(manager, "checkOp", op, uid, packageName));
        } catch (Exception e) {
            e.printStackTrace();
        }
    } else {
        Flog.e("Below API 19 cannot invoke!");
    }
    return false;
}

```

> 
> ReflectUtils.java
> 
> 
> 

```
public static Object invokeMethod(@NonNull Object receiver, String methodName, Object... methodArgs) throws Exception {
Class<?>[] argsClass = null;
    if (methodArgs != null && methodArgs.length != 0) {
        int length = methodArgs.length;
        argsClass = new Class[length];
        for (int i=0; i<length; i++) {
            argsClass[i] = getBaseTypeClass(methodArgs[i].getClass());
        }
    }

    Method method = receiver.getClass().getMethod(methodName, argsClass);
    return method.invoke(receiver, methodArgs);
}

```

[Reference](https://stackoverflow.com/questions/33382436/how-to-check-permission-system-alert-window-is-granted-on-android-lollipop)