Android 8.0的缓存大小和缓存清理接口方法

作者:法迪 时间:2023-11-09 16:47:06 

获取缓存大小接口

主要这里的方法已经和7.0不兼容了。


import android.app.usage.UsageStats;
import android.app.usage.UsageStatsManager;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Process;
import android.os.storage.StorageManager;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Map;

import android.app.usage.StorageStats;
import android.app.usage.StorageStatsManager;

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

try {
     StorageStats storageStats = storageStatsManager.queryStatsForPackage(StorageManager.UUID_DEFAULT, mPackageName, Process.myUserHandle());
     return storageStats.getCacheBytes();
   } catch (PackageManager.NameNotFoundException e) {
     e.printStackTrace();
   } catch (IOException e) {
     e.printStackTrace();
   }
   return 0L;
 }

清除缓存接口

这里的接口和7.0保持一致


       PackageManager mPm = mContext.getPackageManager();
       // need android.permission.DELETE_CACHE_FILES
       mPm.deleteApplicationCacheFiles(cacheItem.packageName, new IPackageDataObserver.Stub() {

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

Andorid 8.0 Setting 模块的源码参考

源码的缓存大小


import com.android.internal.util.Preconditions;
import com.android.settings.utils.AsyncLoader;
import com.android.settingslib.applications.StorageStatsSource;
import com.android.settingslib.applications.StorageStatsSource.AppStorageStats;

import java.io.IOException;

/**
* 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 = mSource.getStatsForPackage(mInfo.volumeUuid, mInfo.packageName, mUser);
   } catch (NameNotFoundException | IOException e) {
     Log.w(TAG, "Package may have been removed during query, failing gracefully", e);
   }
   return result;
 }

========================================================================================
package com.android.settings.applications;

public class AppStorageSettings extends AppInfoWithHeader

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

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

源码的清理缓存接口

mPm.deleteApplicationCacheFiles


package com.android.settings.applications;

public class AppStorageSettings extends AppInfoWithHeader
   implements OnClickListener, Callbacks, DialogInterface.OnClickListener,
   LoaderManager.LoaderCallbacks<AppStorageStats> {
 private static final String TAG = AppStorageSettings.class.getSimpleName();

private ClearCacheObserver mClearCacheObserver;

@Override
 public void onClick(View v) {
   // 清理缓存按钮
   if (v == mClearCacheButton) {
     if (mAppsControlDisallowedAdmin != null && !mAppsControlDisallowedBySystem) {
       RestrictedLockUtils.sendShowAdminSupportDetailsIntent(
           getActivity(), mAppsControlDisallowedAdmin);
       return;
     } else if (mClearCacheObserver == null) { // Lazy initialization of observer
       mClearCacheObserver = new ClearCacheObserver();
     }
     mMetricsFeatureProvider.action(getContext(),
         MetricsEvent.ACTION_SETTINGS_CLEAR_APP_CACHE);
     // 清理缓存
     mPm.deleteApplicationCacheFiles(mPackageName, mClearCacheObserver);

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

Android 7.0 的缓存大小接口


 PackageManager mPm = mContext.getPackageManager();
 mPm.getPackageSizeInfo(mPackageName, new IPackageStatsObserver.Stub() {

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

cacheTotalSize = pStats.cacheSize + pStats.externalCacheSize;
   }
 }

来源:https://blog.csdn.net/su749520/article/details/78914460

标签:Android,8.0,缓存,接口
0
投稿

猜你喜欢

  • c#之事件用法

    2023-09-22 05:05:28
  • string boot 与 自定义interceptor的实例讲解

    2023-10-27 17:03:20
  • springboot+vue制作后台管理系统项目

    2022-02-26 06:08:37
  • Java中ReentrantLock4种常见的坑

    2021-09-26 10:51:46
  • WPF实现雷达图(仿英雄联盟)的示例代码

    2023-07-13 18:46:53
  • C#如何获取枚举的描述属性详解

    2023-02-23 13:49:59
  • c#的sortedlist使用方法

    2023-09-22 04:58:13
  • 深入理解堆排序及其分析

    2022-05-09 00:16:09
  • C#中隐式运行CMD命令行窗口的方法

    2021-06-11 18:47:07
  • Android实现支付宝支付密码输入界面

    2023-06-20 14:57:15
  • Spring Cloud整合XXL-Job的示例代码

    2021-06-09 06:01:59
  • Java中JSR303的基本使用详情

    2021-07-29 01:01:12
  • Android Studio实现简单音乐播放功能的示例代码

    2021-09-14 19:51:45
  • 详解大数据处理引擎Flink内存管理

    2023-03-09 14:41:43
  • Spring依赖注入的三种方式小结

    2022-08-09 15:56:41
  • WPF实现背景灯光随鼠标闪动效果

    2023-09-05 15:39:27
  • Java设计模式之静态代理模式实例分析

    2021-05-30 17:44:19
  • 弹出一个带确认和取消的dialog实例

    2023-11-22 06:07:13
  • SpringBoot Nacos实现自动刷新

    2023-09-16 04:17:09
  • C#中使用强制类型实现字符串和ASCII码之间的转换

    2022-05-15 14:57:48
  • asp之家 软件编程 m.aspxhome.com