Code:
void clearCache() {
if (mClearCacheObserver == null) {
mClearCacheObserver = new CachePackageDataObserver();
}
PackageManager mPM = getPackageManager();
@SuppressWarnings("rawtypes")
final Class[] classes = {Long.TYPE, IPackageDataObserver.class};
Long localLong = Long.valueOf(CACHE_APP);
try {
Method localMethod =
mPM.getClass().getMethod("freeStorageAndNotify", classes);
localMethod.setAccessible(true);
// Start of inner try-catch block
try {
localMethod.invoke(mPM, localLong, mClearCacheObserver);
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.getCause().printStackTrace();
}
// End of inner try-catch block
} catch (NoSuchMethodException e1) {
e1.printStackTrace();
}
}
Logcat:
java.lang.SecurityException: Neither user 10206 nor current process has android.permission.CLEAR_APP_CACHE.
at android.os.Parcel.readException(Parcel.java:1620)
at android.os.Parcel.readException(Parcel.java:1573)
at android.content.pm.IPackageManager$Stub$Proxy.freeStorageAndNotify(IPackageManager.java:5081)
at android.app.ApplicationPackageManager.freeStorageAndNotify(ApplicationPackageManager.java:2500)
at android.content.pm.PackageManager.freeStorageAndNotify(PackageManager.java:4710)
at java.lang.reflect.Method.invoke(Native Method)
at com.onexsoftech.clearcacheapp.MainActivity.clearCache(MainActivity.java:278)
at com.onexsoftech.clearcacheapp.MainActivity.insertDummyContactWrapper1(MainActivity.java:495)
at com.onexsoftech.clearcacheapp.MainActivity.insertDummyContact(MainActivity.java:472)
Requesting Android Runtime Permissions For this the following method needs to be called on every permission. checkSelfPermission(String perm); It returns an integer value of PERMISSION_GRANTED or PERMISSION_DENIED.
Runtime permissions prevent apps from gaining access to private data without a user's consent, and provide them with additional context and visibility into the types of permissions that applications are either seeking, or have been granted.
READ_PHONE_STATE is one of the Android permissions categorized as dangerous. This is because it “allows read only access to phone state, including the phone number of the device, current cellular network information, the status of any ongoing calls, and a list of any Phone Accounts registered on the device” [2] .
Prior to Android 6.0, CLEAR_APP_CACHE
had a protectionLevel
of dangerous
, so ordinary SDK apps could request it in the manifest.
As of Android 6.0, CLEAR_APP_CACHE
has a protectionLevel
of signature|privileged
. Ordinary Android apps cannot hold this permission. You can only hold this permission if your app is signed with the firmware's signing key or you are installed on the privileged system partition.
From Android M -> CLEAR_APP_CACHE, Protection level: system|signature
Android 6.0 does not change the behavior of normal permissions (all non-dangerous permissions including normal, system, and signature permissions).
So it is not possible to ask for that permission in runtime. To be more precise
A signature|system permission, meaning that it can only be held by apps that are signed with the firmware's signing key or are installed on the system partition (e.g., by a rooted device user). From this stackoverflow Q/A.
Docs: https://source.android.com/devices/tech/config/runtime_perms.html#affected-permissions
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With