//检查是否已经授予权限
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
boolean canBackgroundStart = canBackgroundStart(RecipeStartActivity.this);
if (!canBackgroundStart) {
MessageDialog.show(RecipeStartActivity.this, "提示", "为了确保您在后台可以正常完成处方任务,需要您允许显示在其他应用上层的权限", "确定")
.setOnDismissListener(new OnDismissListener() {
@Override
public 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;
}
}
|