IPackageStatsObserver 无法解析为类型 (Android)

IPackageStatsObserver cannot be resolved to a type (Android)

我想计算使用该设备的所有缓存内存的总和。为此,我收到一个编译时间错误:

IPackageStatsObserver cannot be resolved to a type

我在代码中提到过这个错误2次:

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 在 android SDK 中不可用。也许使用 Class.forName() 加载 Stub class,找到默认构造函数并调用它来获取 Stub 的新实例。但是我应该怎么编码,我对此一无所知!?

求助!!

您需要在您的项目中添加IPackageStatsObserver.aidl and PackageStats.aidl

要解决您的问题,请按照以下步骤操作:

Android工作室

  • 右键单击您的项目新建 > 文件夹 > AIDL 文件夹
  • 完成
  • 右键单击 aidl 文件夹 新建 > 包
  • 插入android.content.pm并按确定
  • 下载IPackageStatsObserver.aidl
  • 复制android.content.pm包内的aidl文件
  • 构建 > 重建项目

日食

  • 右键单击 src 文件夹 新建 > 包
  • 插入android.content.pm
  • 完成
  • 下载IPackageStatsObserver.aidl
  • 复制android.content.pm包内的aidl文件
  • Select class 然后 totalCacheSize Source > Organize Imports

已更新

从 Android O(API 级别 26)开始,您不能使用带反射的 getPackageSizeInfo 方法。以下 post 包含一个代码,可以帮助您使用低于 api 级别 26 和更高级别的两个应用程序:

按照以下步骤操作

app > src > main >java > 

在此路径下创建三个文件夹

create folder =  "android"> "content" > "pm"

在 pm 文件夹中粘贴此文件以检索所有系统缓存

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;
}