This is my first post on StackOverflow.
I have used the reflection in my boosting app like below. It works well in sdk<26 but in sdk 26 I get the jav
Updated
From Android O (API level 26) you can not use getPackageSizeInfo method with reflaction. Following is a code that can help you for both below api level 26 and above it:
private void getPackageSizeInfo(final Context context, String packageName) {
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 ai = context.getPackageManager().getApplicationInfo(packageName, 0);
StorageStats storageStats = storageStatsManager.queryStatsForUid(ai.storageUuid, getApplicationInfo().uid);
long cacheSize =storageStats.getCacheBytes();
long dataSize =storageStats.getDataBytes();
long apkSize =storageStats.getAppBytes();
Toast.makeText(context, cacheSize + ",," + dataSize + ",," + apkSize, Toast.LENGTH_LONG).show();
//long size += info.cacheSize;
} catch (Exception e) {
e.printStackTrace();
}
} else {
Method getPackageSizeInfo;
try {
getPackageSizeInfo = context.getPackageManager().getClass()
.getMethod("getPackageSizeInfo",
String.class, Class.forName("android.content.pm.IPackageStatsObserver"));
getPackageSizeInfo.invoke(context.getPackageManager(), packageName,
new IPackageStatsObserver.Stub() { //error
public void onGetStatsCompleted(
PackageStats pStats, boolean succeeded)
throws RemoteException {
//totalSize = totalSize + pStats.cacheSize;
//Log.d("size", totalSize+"");
Toast.makeText(getApplicationContext(), "size"+pStats.cacheSize + ",," + pStats.dataSize + ",," + pStats.codeSize, Toast.LENGTH_SHORT).show();
}
}
);
} catch (Exception e) {
try {
getPackageSizeInfo = context.getPackageManager().getClass()
.getMethod("getPackageSizeInfo",
String.class, Class.forName("android.content.pm.IPackageStatsObserver"));
getPackageSizeInfo.invoke(context.getPackageManager(), packageName,
new IPackageStatsObserver.Stub() { //error
public void onGetStatsCompleted(
PackageStats pStats, boolean succeeded)
throws RemoteException {
Toast.makeText(getApplicationContext(), "size"+pStats.cacheSize, Toast.LENGTH_SHORT).show();
}
}
);
} catch (Exception ee) {
Log.d("eeeeeeeeeee", "error");
ee.printStackTrace();
}
}
}
}
Please notice that for API level below 26 for making the code work, you should configure AIDL and the requirments according to: https://stackoverflow.com/a/30278018/1939409
Relplace
ApplicationInfo ai = context.getPackageManager().getApplicationInfo(packageName, 0);
StorageStats storageStats = storageStatsManager.queryStatsForUid(ai.storageUuid, getApplicationInfo().uid);
long cacheSize =storageStats.getCacheBytes();
long dataSize =storageStats.getDataBytes();
long apkSize =storageStats.getAppBytes();
Toast.makeText(context, cacheSize + ",," + dataSize + ",," + apkSize, Toast.LENGTH_LONG).show();
//long size += info.cacheSize;
with
ApplicationInfo ai = AppController.getContext().getPackageManager().getApplicationInfo(packageName, 0);
StorageStats storageStats = storageStatsManager.queryStatsForUid(ai.storageUuid, AppController.getContext().getApplicationInfo().uid);
long cacheSize = storageStats.getCacheBytes();
long dataSize = storageStats.getDataBytes();
long apkSize = storageStats.getAppBytes();
PackageStats packageStats = new PackageStats(packageName);
packageStats.cacheSize = cacheSize;
packageStats.dataSize = dataSize;
packageStats.codeSize = apkSize;
observer.onGetStatsCompleted(packageStats, true);