How to accept CLEAR_APP_CACHE permission in Android Marshmallow at runtime?

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)
+4
source share
3 answers

Prior to Android 6.0, it CLEAR_APP_CACHEhad protectionLevelonedangerous , so regular SDK applications could request it in the manifest.

Starting with Android 6.0 has out . Regular Android apps cannot use this permission. You can use this permission only if your application is signed using the firmware signing key or installed in a privileged system partition. CLEAR_APP_CACHEprotectionLevelsignature|privileged

+5
source

Android M → CLEAR_APP_CACHE, : system | signature

Android 6.0 ( , , ).

, .

| , , , (, ). fooobar.com/questions/248410/...

Docs: https://source.android.com/devices/tech/config/runtime_perms.html#affected-permissions

+3

Add permission to AndroidManifest.xml

<permission android:name="android.permission.CLEAR_APP_CACHE"/>
<uses-permission android:name="android.permission.CLEAR_APP_CACHE"/>

Create a constant for the request code.

Constants.java

public static final int REQUEST_CODE_FOR_PERMISSION = 501;

Request Resolution: -

public static void requestPermissionForClearCache(Activity activity) {
    if (ActivityCompat.checkSelfPermission(activity, Manifest.permission.CLEAR_APP_CACHE) != PackageManager.PERMISSION_GRANTED) {
        if (ActivityCompat.shouldShowRequestPermissionRationale(activity, Manifest.permission.CLEAR_APP_CACHE)) {
            ActivityCompat.requestPermissions(activity, new String[]{Manifest.permission.CLEAR_APP_CACHE}, Constatnts.REQUEST_CODE_FOR_PERMISSION);
        } else {
            ActivityCompat.requestPermissions(activity, new String[]{Manifest.permission.CLEAR_APP_CACHE}, Constatnts.REQUEST_CODE_FOR_PERMISSION);
        }
    }
}

Override the method below in the snippet.

@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
    if (requestCode == Constatnts.REQUEST_CODE_FOR_PERMISSION && grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
        // permission was granted successfully
    } else {
        // permission was NOT granted successfully
    }
}
-3
source

Source: https://habr.com/ru/post/1658925/


All Articles