IPackageStatsObserver无法解析为某个类型(Android) [英] IPackageStatsObserver cannot be resolved to a type (Android)

查看:728
本文介绍了IPackageStatsObserver无法解析为某个类型(Android)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我在代码中已经提到过2次错误:

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

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!?

帮助!!

推荐答案

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

To resolve your issue follow these steps:

Android Studio


  • 右键单击您的项目新建>文件夹> AIDL文件夹

  • 完成

  • 右键单击 aidl 文件夹新建>包

  • 插入 android.content.pm 然后按确定

  • 下载 IPackageStatsObserver.aidl

  • 将aidl文件复制到 android.content.pm 包中

  • 构建>重建项目

  • Right click on your project New > Folder > AIDL Folder
  • Press Finish
  • Right click on aidl folder New > Package
  • Insert android.content.pm and press OK
  • Download IPackageStatsObserver.aidl
  • Copy the aidl file inside the android.content.pm package
  • Build > Rebuild Project

Eclipse


  • 右键单击 rc 文件夹新建>套餐

  • 插入 android.content.pm

  • 完成

  • 下载 IPackageStatsObserver.aidl

  • 将aidl文件复制到<$ c $里面c> android.content.pm package

  • 选择 totalCacheSize 的类 Source>组织进口

  • Right click on src folder New > Package
  • Insert android.content.pm
  • Press Finish
  • Download IPackageStatsObserver.aidl
  • Copy the aidl file inside the android.content.pm package
  • Select class where totalCacheSize is then Source > Organize Imports

这篇关于IPackageStatsObserver无法解析为某个类型(Android)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆