Reputation: 1504
I want to calculate the sum of all the cache memory utilizing the device. For this I am getting a compiled time error:
IPackageStatsObserver cannot be resolved to a type
I have mentioned this error 2 times in the code:
public long totalCacheSize(){
totalSize=0;
PackageManager packageManager = getApplicationContext().getPackageManager();
/* List<PackageInfo> packs = packageManager
.getInstalledPackages(PackageManager.GET_ACTIVITIES);*/
// PackageManager.GET_META_DATA
List<PackageInfo> packs = packageManager
.getInstalledPackages(PackageManager.GET_META_DATA);
for (int i = 0; i < packs.size(); i++) {
PackageInfo p = packs.get(i);
Method getPackageSizeInfo;
try {
getPackageSizeInfo = packageManager.getClass()
.getMethod("getPackageSizeInfo",
String.class, Class.forName("android.content.pm.IPackageStatsObserver"));
getPackageSizeInfo.invoke(packageManager, p.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"+totalSize, Toast.LENGTH_SHORT).show();
}
}
);
} catch (Exception e) {
try {
getPackageSizeInfo = packageManager.getClass()
.getMethod("getPackageSizeInfo",
String.class, Class.forName("android.content.pm.IPackageStatsObserver"));
getPackageSizeInfo.invoke(packageManager, p.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"+totalSize, Toast.LENGTH_SHORT).show();
}
}
);
} catch (Exception ee) {
Log.d("eeeeeeeeeee", "error");
ee.printStackTrace();
}
}
}
Log.d("return size", totalSize+"");
Toast.makeText(getApplicationContext(), "return size"+totalSize, Toast.LENGTH_SHORT).show();
return totalSize;
}
IPackageStatsObserver
is not available in android SDK. Perhaps loading the Stub class using Class.forName()
, finding the default constructor and invoke it to get a new instance of a Stub
. But what should I code it, I do not know anything about it!?
Help !!
Upvotes: 6
Views: 9566
Reputation: 761
follow steps below
app > src > main >java >
in this path create three folders
create folder = "android"> "content" > "pm"
in pm folder paste this file for retrieving all system caches
IPackageDataObserver.java
package android.content.pm;
import android.os.Binder;
import android.os.IBinder;
import android.os.IInterface;
import android.os.Parcel;
import android.os.RemoteException;
public interface IPackageDataObserver extends IInterface {
public static abstract class Stub extends Binder implements IPackageDataObserver {
private static final String DESCRIPTOR = "android.content.pm.IPackageDataObserver";
static final int TRANSACTION_onRemoveCompleted = 1;
private static class Proxy implements IPackageDataObserver {
private IBinder mRemote;
public String getInterfaceDescriptor() {
return Stub.DESCRIPTOR;
}
Proxy(IBinder iBinder) {
this.mRemote = iBinder;
}
public IBinder asBinder() {
return this.mRemote;
}
public void onRemoveCompleted(String str, boolean z) throws RemoteException {
Parcel obtain = Parcel.obtain();
try {
obtain.writeInterfaceToken(Stub.DESCRIPTOR);
obtain.writeString(str);
obtain.writeInt(z ? 1 : 0);
this.mRemote.transact(1, obtain, null, 1);
} finally {
obtain.recycle();
}
}
}
public IBinder asBinder() {
return this;
}
public Stub() {
attachInterface(this, DESCRIPTOR);
}
public static IPackageDataObserver asInterface(IBinder iBinder) {
if (iBinder == null) {
return null;
}
IInterface queryLocalInterface = iBinder.queryLocalInterface(DESCRIPTOR);
if (queryLocalInterface == null || !(queryLocalInterface instanceof IPackageDataObserver)) {
return new Proxy(iBinder);
}
return (IPackageDataObserver) queryLocalInterface;
}
public boolean onTransact(int i, Parcel parcel, Parcel parcel2, int i2) throws RemoteException {
String str = DESCRIPTOR;
if (i == 1) {
parcel.enforceInterface(str);
onRemoveCompleted(parcel.readString(), parcel.readInt() != 0);
return true;
} else if (i != 1598968902) {
return super.onTransact(i, parcel, parcel2, i2);
} else {
parcel2.writeString(str);
return true;
}
}
}
void onRemoveCompleted(String str, boolean z) throws RemoteException;
}
Upvotes: 0
Reputation: 5462
Updated
From Android O (API level 26) you can not use getPackageSizeInfo
method with reflaction. Following post includes a code that can help you for both apps below api level 26 and above it:
https://stackoverflow.com/a/56616172/1939409
Upvotes: 0
Reputation: 32780
To resolve your issue follow these steps:
Android Studio
aidl
folder New > Packageandroid.content.pm
and press OKandroid.content.pm
packageEclipse
src
folder New > Packageandroid.content.pm
android.content.pm
packagetotalCacheSize
is then Source > Organize ImportsUpvotes: 16
Reputation: 15313
you need to add IPackageStatsObserver.aidl and PackageStats.aidl in your project.
Upvotes: 1