时间:2021-05-21
获取缓存大小接口
主要这里的方法已经和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; } }以上这篇Android 8.0的缓存大小和缓存清理接口方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
一、简介:Picasso是Square公司开源的一个Android图形缓存库。可以实现图片下载和缓存功能。二、Picasso的特性Picasso是一个Andro
thinkPHP的F方法只能用于缓存简单数据类型,不支持有效期和缓存对象。S()缓存方法支持有效期,又称动态缓存方法,使用示例如下:复制代码代码如下://使用d
android之清理缓存实现,供大家参考,具体内容如下一.清理缓存首先要搞清楚清理哪些东西1、app本身的功能比如录像,录音,更新都会产生文件,需要清理2、ap
qq占用内存太大可以通过清理缓存减少占用内存,清理缓存的方法如下: 1、打开手机QQ并点击头像。 2、点击设置,空间清理,QQ缓存数据清理。 3、按照需要
PHPYii清理缓存的实现方法本文实例讲述了Yii清理缓存的方法。分享给大家供大家参考,具体如下:html:ClearCachejs:functionclear