SoFunction
Updated on 2025-04-09

Android 8.0 cache size and cache cleaning interface method

Get cache size interface

The main method here is incompatible with 7.0.

import ;
import ;
import ;
import ;
import ;
import ;

import ;
import ;
import ;

import ;
import ;

  public static long getCacheSizeByAndroidO(Context mContext, String mPackageName) {
    StorageManager storageManager = (StorageManager) (Context.STORAGE_SERVICE);
    StorageStatsManager storageStatsManager = (StorageStatsManager) (Context.STORAGE_STATS_SERVICE);

    try {
      StorageStats storageStats = (StorageManager.UUID_DEFAULT, mPackageName, ());
      return ();
    } catch ( e) {
      ();
    } catch (IOException e) {
      ();
    }
    return 0L;
  }

Clear the cache interface

The interface here is consistent with 7.0

        PackageManager mPm = ();
        // need .DELETE_CACHE_FILES
        (, new () {

          @Override
          public void onRemoveCompleted(final String packageName, final boolean succeeded) throws RemoteException {
              ///
            }
          }
        });

Source code reference for Android 8.0 Setting module

Source code cache size

import ;
import ;
import ;
import ;

import ;

/**
 * Fetches the storage stats using the StorageStatsManager for a given package and user tuple.
 */
public class FetchPackageStorageAsyncLoader extends AsyncLoader<AppStorageStats> {
  private static final String TAG = "FetchPackageStorage";
  private final StorageStatsSource mSource;
  private final ApplicationInfo mInfo;
  private final UserHandle mUser;

  @Override
  public AppStorageStats loadInBackground() {
    AppStorageStats result = null;
    try {
      result = (, , mUser);
    } catch (NameNotFoundException | IOException e) {
      (TAG, "Package may have been removed during query, failing gracefully", e);
    }
    return result;
  }

========================================================================================
package ;

public class AppStorageSettings extends AppInfoWithHeader

  @Override
  public void onLoadFinished(Loader<AppStorageStats> loader, AppStorageStats result) {
    (result);
    updateUiWithSize(result);
  }

    private void updateUiWithSize(AppStorageStats result) {
      } else {
      long codeSize = ();
      long cacheSize = ();
      long dataSize = () - cacheSize;

Source code cleaning cache interface

package ;

public class AppStorageSettings extends AppInfoWithHeader
    implements OnClickListener, Callbacks, ,
    &lt;AppStorageStats&gt; {
  private static final String TAG = ();

  private ClearCacheObserver mClearCacheObserver;

  @Override
  public void onClick(View v) {
    // Clean the cache button    if (v == mClearCacheButton) {
      if (mAppsControlDisallowedAdmin != null &amp;&amp; !mAppsControlDisallowedBySystem) {
        (
            getActivity(), mAppsControlDisallowedAdmin);
        return;
      } else if (mClearCacheObserver == null) { // Lazy initialization of observer
        mClearCacheObserver = new ClearCacheObserver();
      }
      (getContext(),
          MetricsEvent.ACTION_SETTINGS_CLEAR_APP_CACHE);
      // Clean up the cache      (mPackageName, mClearCacheObserver);

  class ClearCacheObserver extends  {
    public void onRemoveCompleted(final String packageName, final boolean succeeded) {
      final Message msg = (MSG_CLEAR_CACHE);
      msg.arg1 = succeeded ? OP_SUCCESSFUL : OP_FAILED;
      (msg);
    }
  }

Android 7.0 cache size interface

  PackageManager mPm = ();
  (mPackageName, new () {

    @Override
    public void onGetStatsCompleted(PackageStats pStats, boolean succeeded) throws RemoteException {

      cacheTotalSize =  + ;
    }
  }

The above article on Android 8.0's cache size and cache cleaning interface method are all the content I have shared with you. I hope you can give you a reference and I hope you can support me more.