Created
August 30, 2019 07:19
-
-
Save thoinv/d4fffd249b2b5f80b6eb78cf6585f19f to your computer and use it in GitHub Desktop.
Permission Util #android #utils #permission
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@SuppressLint("WrongConstant") | |
public static boolean canDrawOverlays(Context context) { | |
if (Build.VERSION.SDK_INT >= 23) { | |
return Settings.canDrawOverlays(context); | |
} else { | |
if (Build.MANUFACTURER.equalsIgnoreCase("xiaomi")) { | |
return true; | |
} | |
AppOpsManager appOpsManager = (AppOpsManager) context.getSystemService("appops"); | |
Method method = null; | |
try { | |
if (Build.VERSION.SDK_INT >= 19) { | |
method = AppOpsManager.class.getMethod("checkOpNoThrow", Integer.TYPE, Integer.TYPE, String.class); | |
} | |
method.setAccessible(true); | |
return (Integer) method.invoke(appOpsManager, 24, Binder.getCallingUid(), context.getPackageName()) == 0; | |
} catch (Exception e) { | |
e.printStackTrace(); | |
return true; | |
} | |
} | |
} | |
public static boolean openSettingPermission(Context context, Intent intent) { | |
if (context.getPackageManager().queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY).size() > 0) { | |
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); | |
context.startActivity(intent); | |
return true; | |
} | |
new StringBuilder("Intent is not available! ").append(intent); | |
return false; | |
} | |
public static boolean allow(Context context, String... permissions) { | |
if (Build.VERSION.SDK_INT < 23) { | |
return true; | |
} | |
return PermissionUtils.hasSelfPermissions(context, permissions); | |
} | |
public static List<String> getPermissionsNotAllow(Context context, String... permissions) { | |
List<String> permissionList = new ArrayList<>(); | |
for (String str : permissions) { | |
if (!allow(context, str)) { | |
permissionList.add(str); | |
} | |
} | |
return permissionList; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment