/** * @param context Any context. * @return Whether the package has the WAKE_LOCK permission. */ public static boolean hasWakeLockPermission(Context context) { return hasPermission(context, Manifest.permission.WAKE_LOCK, 0); }
/** * @param context Any context. * @return Whether the package has the RECEIVE_BOOT_COMPLETED permission. */ public static boolean hasBootPermission(Context context) { return hasPermission(context, Manifest.permission.RECEIVE_BOOT_COMPLETED, 0); }
private static boolean hasPermission(Context context, String permission, int repeatCount) { try { return PackageManager.PERMISSION_GRANTED == context.getPackageManager() .checkPermission(permission, context.getPackageName()); } catch (Exception e) { CAT.e(e); // crash https://gist.github.com/vRallev/6affe17c93e993681bfd // give it another chance with the application context return repeatCount < 1 && hasPermission(context.getApplicationContext(), permission, repeatCount + 1); } } }
/** * @param context Any context. * @return Whether the package has the WAKE_LOCK permission. */ public static boolean hasWakeLockPermission(Context context) { return hasPermission(context, Manifest.permission.WAKE_LOCK, 0); }
/** * @param context Any context. * @return Whether the package has the RECEIVE_BOOT_COMPLETED permission. */ public static boolean hasBootPermission(Context context) { return hasPermission(context, Manifest.permission.RECEIVE_BOOT_COMPLETED, 0); }
private static boolean hasPermission(Context context, String permission, int repeatCount) { try { return PackageManager.PERMISSION_GRANTED == context.getPackageManager() .checkPermission(permission, context.getPackageName()); } catch (Exception e) { CAT.e(e); // crash https://gist.github.com/vRallev/6affe17c93e993681bfd // give it another chance with the application context return repeatCount < 1 && hasPermission(context.getApplicationContext(), permission, repeatCount + 1); } } }