android 获取系统缓存之 java.lang.reflect.InvocationTargetException
今天我给大家分享一个关于获取android手机应用缓存信息的方法以及androidO以上的版本适配问题。如果不做适配兼容的话就会报java.lang.reflect.InvocationTargetException错误。
以前我们在获取的方法如下:
public void getPackageSizeInfo(String packageName, IPackageStatsObserver.Stub observer) {
Context context = App.getAppContext();
try {
PackageManager pm = App.sContext.getPackageManager();
Method method = pm.getClass()
.getMethod("getPackageSizeInfo", String.class, IPackageStatsObserver.class);
method.invoke(pm, packageName, observer);
} catch (Exception e) {
e.printStackTrace();
}
}
private class PackageStatsObserver extends IPackageStatsObserver.Stub {
@Override
public void onGetStatsCompleted(PackageStats pStats, boolean succeeded) throws RemoteException {
Log.d("guojingbu", "packageName = "+pStats.packageName+"---"+(pStats.cacheSize + pStats.externalCacheSize));
}
}
//使用
getPackageSizeInfo(“应用包名”,new PackageStatsObserver ());
androidO以及以上如果再用上面的方法就会 报java.lang.reflect.InvocationTargetException错误。
androidO以上我们应该用下面的方法
@RequiresApi(api = Build.VERSION_CODES.O)
private void getPackageSizeInfo(String packageName, IPackageStatsObserver.Stub observer) {
Context context = App.getAppContext();
final StorageStatsManager storageStatsManager = (StorageStatsManager) context.getSystemService(Context.STORAGE_STATS_SERVICE);
try {
ApplicationInfo applicationInfo = context.getPackageManager().getApplicationInfo(packageName, 0);
StorageStats storageStats = storageStatsManager.queryStatsForUid(applicationInfo.storageUuid, applicationInfo.uid);
long cacheSize = storageStats.getCacheBytes();
long dataSize = storageStats.getDataBytes();
long apkSize = storageStats.getAppBytes();
Log.i("guojingbu", cacheSize + "---" + dataSize + "---" + apkSize);
//long size += info.cacheSize;
} catch (Exception e) {
e.printStackTrace();
}
}
最后我们贴一下适配完整的方法:
private void getPackageSizeInfo(String packageName, IPackageStatsObserver.Stub observer) {
Context context = App.getAppContext();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
final StorageStatsManager storageStatsManager = (StorageStatsManager) context.getSystemService(Context.STORAGE_STATS_SERVICE);
final StorageManager storageManager = (StorageManager) context.getSystemService(Context.STORAGE_SERVICE);
try {
ApplicationInfo applicationInfo = context.getPackageManager().getApplicationInfo(packageName, 0);
StorageStats storageStats = storageStatsManager.queryStatsForUid(applicationInfo.storageUuid, applicationInfo.uid);
long cacheSize = storageStats.getCacheBytes();
long dataSize = storageStats.getDataBytes();
long apkSize = storageStats.getAppBytes();
Log.i("guojingbu", cacheSize + ",," + dataSize + ",," + apkSize);
} catch (Exception e) {
e.printStackTrace();
}
} else {
try {
PackageManager pm = App.sContext.getPackageManager();
Method method = pm.getClass()
.getMethod("getPackageSizeInfo", String.class, IPackageStatsObserver.class);
method.invoke(pm, packageName, observer);
} catch (Exception e) {
Log.d("guojingbu", "error");
e.printStackTrace();
}
}
}
private class PackageStatsObserver extends IPackageStatsObserver.Stub {
@Override
public void onGetStatsCompleted(PackageStats pStats, boolean succeeded) throws RemoteException {
Log.d("guojingbu", "packageName = "+pStats.packageName+"---"+(pStats.cacheSize + pStats.externalCacheSize));
}
}
今天的内容就分享完了。主要目的也是自己做一点笔记以便以后查阅。