Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
270 views
in Technique[技术] by (71.8m points)

java - IPackageStatsObserver cannot be resolved to a type (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

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

To resolve your issue follow these steps:

Android Studio

  • 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

  • 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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...