如何检查MIUI中的显示弹出窗口(WindowManager)权限?

时间:2016-07-20 13:30:26

标签: android user-permissions android-windowmanager

我想检查是否授予WindowManager权限。 我获得了重定向到MIUI from here的用户权限activity的解决方案。

我正在检查WindowManager权限,但它抛出 java.lang.IllegalArgumentException:此行未知操作字符串:android:system_alert_window

final int mode = manager.checkOp(AppOpsManager.OPSTR_SYSTEM_ALERT_WINDOW,                         Binder.getCallingUid(),context.getPackageName());

  @SuppressLint("NewApi")
public static boolean isFloatWindowOpAllowed(Context context) {

        if (Build.VERSION.SDK_INT >= 19) { // 19, 4.4, KITKAT

            final AppOpsManager manager = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE);
            final int mode = manager.checkOp(AppOpsManager.OPSTR_SYSTEM_ALERT_WINDOW,
                    Binder.getCallingUid(), context.getPackageName());
            return AppOpsManager.MODE_ALLOWED == mode;
        } else {
            return (context.getApplicationInfo().flags & 1 << 27) == 1;
        }

}

错误日志:

08-23 18:18:25.733: W/System.err(23108): java.lang.IllegalArgumentException: Unknown operation string: android:system_alert_window
08-23 18:18:25.733: W/System.err(23108):    at android.app.AppOpsManager.strOpToOp(AppOpsManager.java:1624)
08-23 18:18:25.733: W/System.err(23108):    at android.app.AppOpsManager.checkOp(AppOpsManager.java:1646)
08-23 18:18:25.733: W/System.err(23108):    at com.windowmanager.demo.util.MIUIUtils.isFloatWindowOpAllowed(MIUIUtils.java:73)
08-23 18:18:25.733: W/System.err(23108):    at com.windowmanager.demo.compat.WindowManagerPermissionCompat$MIUIRomCompatImpl.hasFloatWindowPermission(WindowManagerPermissionCompat.java:163)
08-23 18:18:25.733: W/System.err(23108):    at com.windowmanager.demo.compat.WindowManagerPermissionCompat.hasFloatWindowPermission(WindowManagerPermissionCompat.java:198)
08-23 18:18:25.733: W/System.err(23108):    at com.windowmanager.demo.addons.settings.WindowManagerActivity.onCreate(WindowManagerActivity.java:92)
08-23 18:18:25.733: W/System.err(23108):    at android.app.Activity.performCreate(Activity.java:6041)
08-23 18:18:25.733: W/System.err(23108):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1109)
08-23 18:18:25.733: W/System.err(23108):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2285)
08-23 18:18:25.733: W/System.err(23108):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2394)
08-23 18:18:25.733: W/System.err(23108):    at android.app.ActivityThread.access$800(ActivityThread.java:154)
08-23 18:18:25.733: W/System.err(23108):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1308)
08-23 18:18:25.733: W/System.err(23108):    at android.os.Handler.dispatchMessage(Handler.java:102)
08-23 18:18:25.733: W/System.err(23108):    at android.os.Looper.loop(Looper.java:135)
08-23 18:18:25.733: W/System.err(23108):    at android.app.ActivityThread.main(ActivityThread.java:5276)
08-23 18:18:25.733: W/System.err(23108):    at java.lang.reflect.Method.invoke(Native Method)
08-23 18:18:25.733: W/System.err(23108):    at java.lang.reflect.Method.invoke(Method.java:372)
08-23 18:18:25.733: W/System.err(23108):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:908)
08-23 18:18:25.733: W/System.err(23108):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:703)

1 个答案:

答案 0 :(得分:0)

我认为在这种情况下你需要使用checkOp(int op,int uid,String packageName)而不是checkOp(String op,int uid,String packageName) 但是我不认为checkOp(int,int,String)是不可访问的。您必须使用反射来调用此方法。

AppOpsManager manager = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE);
Class localClass = manager.getClass();
Class[] arrayOfClass = new Class[3];
arrayOfClass[0] = Integer.TYPE;
arrayOfClass[1] = Integer.TYPE;
arrayOfClass[2] = String.class;
try {
    Method method = localClass.getMethod("checkOp", arrayOfClass);
    if (method == null) {
        return false;
    }
    Object[] arrayOfObjects = new Object[3];
    arrayOfObjects[0] = Integer.valueOf(24);
    arrayOfObjects[1] = Integer.valueOf(Binder.getCallingUid());
    arrayOfObjects[2] = context.getPackageName();
    int m = ((Integer) method.invoke((Object) manager, arrayOfObjects)).intValue();
    return m == AppOpsManager.MODE_ALLOWED;
}catch (Exception e){
    return false;
}

OPINT 24用于SYSTEM_ALERT_WINDOW