I am using PackageStats and on printing the PackageStats's codeSize, cacheSize, dataSize, some data is coming as negative.
A few questions:
Also, for Android N it gives me "NoSuchMethodException".So,
Code:
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;
};
}
just tried on API 23/24 and can just instance it.
these external*
properties might refer to the SD card.
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With