我正在使用PackageStats并在打印PackageStats的codeSize,cacheSize,dataSize时,一些数据将显示为负数。
几个问题:
另外,对于Android N,它给了我“NoSuchMethodException”.So,
代码:
PackageManager packageManager = context.getPackageManager();
Method myUserId = UserHandle.class.getDeclaredMethod("myUserId");
int userID = (Integer) myUserId.invoke(packageManager);
Method getPackageSizeInfo = packageManager.getClass().getDeclaredMethod("getPackageSizeInfo", String.class, int.class,
IPackageStatsObserver.class);
getPackageSizeInfo.invoke(packageManager, context.getPackageName(), userID, new IPackageStatsObserver.Stub() {
@Override
public void onGetStatsCompleted(PackageStats pStats, boolean succeeded) throws RemoteException {
long codeSize = pStats.codeSize / MB; //long MB = 1024*1024;
long cacheSize = pStats.cacheSize / MB;
long dataSize = pStats.dataSize / MB;
long appSize = codeSize + cacheSize + dataSize;
};
}
答案 0 :(得分:1)
刚刚尝试使用API 23/24并且可以实例化它。
这些external*
属性可能会引用SD卡。
PackageStats stats = new PackageStats(context.getPackageName());
long codeSize = stats.codeSize + stats.externalCodeSize;
long dataSize = stats.dataSize + stats.externalDataSize;
long cacheSize = stats.cacheSize + stats.externalCacheSize;
long appSize = codeSize + dataSize + cacheSize;