How to handle SYSTEM_ALERT_WINDOW permission not being auto-granted on some pre-Marshmallow devices

后端 未结 5 573
执笔经年
执笔经年 2020-11-30 01:24

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 posts out there r

5条回答
  •  -上瘾入骨i
    2020-11-30 02:02

    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

    Reference

提交回复
热议问题