Crash - Securityexception: Permission Denial: Isuidactive From Pid=xxxx, Uid=xxxx Requires Android.permission.package_usage_stats
I'm getting an unknown crash from Crashlytics which triggered on calling enableReaderMode() in NfcAdapter Caused by java.lang.SecurityException: Permission Denial: isUidActive from
Solution 1:
I don't know why it's requesting that but there is such permission:
<uses-permissionandroid:name="android.permission.PACKAGE_USAGE_STATS"tools:ignore="ProtectedPermissions" />
You can check if your app has it by calling:
publicstaticbooleancheckForUsagePermission(Context context) {
@SuppressLint("WrongConstant")UsageStatsManagerusm= (UsageStatsManager) context.getSystemService("usagestats");
longtime= System.currentTimeMillis();
return usm.queryUsageStats(UsageStatsManager.INTERVAL_DAILY, time - 1000 * 1000, time).size() > 0;
}
And, of course, you can request it by:
IntentsettingsIntent=newIntent(Settings.ACTION_USAGE_ACCESS_SETTINGS);
startActivityForResult(settingsIntent, REQUEST_STATS_USAGE);
Solution 2:
Found that, special permissions that are granted by the user in the system settings (usage stats access, notification access, …) are handled by AppOpsManager
, which was added in Android 4.4. Therefore, I'm adding an extra check as follows
val mode = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
(getSystemService(Context.APP_OPS_SERVICE) as AppOpsManager).unsafeCheckOpNoThrow(
AppOpsManager.OPSTR_GET_USAGE_STATS,
Process.myUid(),
packageName
)
} elsenullval granted = if (mode != null && mode != AppOpsManager.MODE_DEFAULT) {
mode == AppOpsManager.MODE_ALLOWED
} else {
checkCallingOrSelfPermission(Manifest.permission.PACKAGE_USAGE_STATS) == PackageManager.PERMISSION_GRANTED
}
Post a Comment for "Crash - Securityexception: Permission Denial: Isuidactive From Pid=xxxx, Uid=xxxx Requires Android.permission.package_usage_stats"