我一直在收到一些小米设备(例如Mi 2,运行API级别21)没有显示叠加层的报告.我的应用针对API 23. 关于此,有several posts.似乎MIUI设备在安装时不启用此权限(与其他Marshmallow之前的设备不同).
关于此,有several posts.似乎MIUI设备在安装时不启用此权限(与其他Marshmallow之前的设备不同).
不幸的是,Settings.canDrawOverlays()仅适用于Android 23.
>检查Marshmallow之前是否尚未启用此权限的正确方法是什么?
>是否有意图将用户带到相关的MUIU设置页面?也许:新的Intent(“android.settings.action.MANAGE_OVERLAY_PERMISSION”,packageName)但我无法测试这个.
@SuppressLint("NewApi") public static boolean canDrawOverlayViews(Context con){ if(Build.VERSION.SDK_INT< Build.VERSION_CODES.LOLLIPOP){return true;} try { return Settings.canDrawOverlays(con); } catch(NoSuchMethodError e){ return canDrawOverlaysUsingReflection(con); } } public static boolean canDrawOverlaysUsingReflection(Context context) { try { AppOpsManager manager = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE); Class clazz = AppOpsManager.class; Method dispatchMethod = clazz.getMethod("checkOp", new Class[] { int.class, int.class, String.class }); //AppOpsManager.OP_SYSTEM_ALERT_WINDOW = 24 int mode = (Integer) dispatchMethod.invoke(manager, new Object[] { 24, Binder.getCallingUid(), context.getApplicationContext().getPackageName() }); return AppOpsManager.MODE_ALLOWED == mode; } catch (Exception e) { return false; } }
自定义ROM可以更改操作系统,以便Settings.canDrawOverlays()不可用.小米设备和应用程序崩溃后发生了这种情况.
申请许可:
@SuppressLint("InlinedApi") public static void requestOverlayDrawPermission(Activity act, int requestCode){ Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, Uri.parse("package:" + act.getPackageName())); act.startActivityForResult(intent, requestCode); }