//检查是否已经授予权限if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {boolean canBackgroundStart = canBackgroundStart(RecipeStartActivity.this);if (!canBackgroundStart) {MessageDialog.show(RecipeStartActivity.this, "提示", "为了确保您在后台可以正常完成处方任务,需要您允许显示在其他应用上层的权限", "确定").setOnDismissListener(new OnDismissListener() {@Overridepublic void onDismiss() {}});}}public static boolean canBackgroundStart(Context context) {AppOpsManager ops = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE);int op = 10021; // >= 23//ops.checkOpNoThrow(op, Binder.getCallingUid(), context.getPackageName());Method method = null;try {method = ops.getClass().getMethod("checkOpNoThrow", new Class[] {int.class, int.class, String.class} );Integer result = (Integer) method.invoke(ops, op, Binder.getCallingUid(), context.getPackageName());return result == AppOpsManager.MODE_ALLOWED;} catch (Exception e) {e.printStackTrace();return false;}}
如果觉得《Android判断小米后台开启界面的权限》对你有帮助,请点赞、收藏,并留下你的观点哦!