Android8.0中怎么实现缓存大小和缓存清理接口
Android 8.0中怎么实现缓存大小和缓存清理接口?很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。
创新互联建站自2013年创立以来,先为雨城等服务建站,雨城等地企业,进行企业商务咨询服务。为雨城企业网站制作PC+手机+微官网三网同步一站式服务解决您的所有建站问题。
获取缓存大小接口
主要这里的方法已经和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{ 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 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{ 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; } }
看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注创新互联行业资讯频道,感谢您对创新互联的支持。
当前名称:Android8.0中怎么实现缓存大小和缓存清理接口
当前网址:http://scyanting.com/article/jsidso.html