计算android中的总app缓存大小

时间:2013-05-27 12:05:35

标签: android caching

我正在尝试计算所有缓存大小。我的代码如下..

   totalSize=0;

    PackageManager packageManager = context.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,
                    IPackageStatsObserver.class);

            getPackageSizeInfo.invoke(packageManager, p.packageName,
                    new IPackageStatsObserver.Stub() {

                        @Override
                        public void onGetStatsCompleted(
                                PackageStats pStats, boolean succeeded)
                                throws RemoteException {

                            totalSize = totalSize + pStats.cacheSize;
                            Log.d("size", totalSize+"");
                            Toast.makeText(context, "size"+totalSize, Toast.LENGTH_SHORT).show();
                        }
                    }

            );
        } catch (Exception e) {
            try {
                getPackageSizeInfo = packageManager.getClass().getMethod(
                        "getPackageSizeInfo", String.class,
                        IPackageStatsObserver.class);

                getPackageSizeInfo.invoke(packageManager, p.packageName,
                        new IPackageStatsObserver.Stub() {

                            @Override
                            public void onGetStatsCompleted(
                                    PackageStats pStats, boolean succeeded)
                                    throws RemoteException {

                                totalSize = totalSize + pStats.cacheSize;
                                Log.d("size", totalSize+"");
                                Toast.makeText(context, "size"+totalSize, Toast.LENGTH_SHORT).show();
                            }
                        }

                );
            } catch (Exception ee) {
                Log.d("eeeeeeeeeee", "error");
                ee.printStackTrace();
            } 
        } 

    }
    Log.d("return size", totalSize+"");
    Toast.makeText(context, "return size"+totalSize, Toast.LENGTH_SHORT).show();
    return totalSize;

这里我得到每个包的缓存大小。问题是首先返回totalSize的值,然后调用onGetStatsCompleted()函数。所以我总是得到totalSize的0值。

它也给出例外: -

JavaBinder(525): * 未捕获的远程异常! (进程之间尚不支持例外。) JavaBinder(525):java.lang.RuntimeException:无法在未调用Looper.prepare()的线程内创建处理程序

任何人都可以帮助我吗?

1 个答案:

答案 0 :(得分:0)

从回调Toast方法中删除onGetStatsCompleted()。你将摆脱异常

用这样的代码替换你的代码并检查它是否适合你。

private int getTotalSize() {
        totalSize = 0;

        PackageManager packageManager = getApplicationContext().getPackageManager();

        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, IPackageStatsObserver.class);
                getPackageSizeInfo.invoke(packageManager, p.packageName, new IPackageStatsObserver.Stub() {
                    @Override
                    public void onGetStatsCompleted(PackageStats pStats, boolean succeeded) throws RemoteException {
                        totalSize = (int) (totalSize + pStats.cacheSize);
                    }
                });
            } catch (Exception e) {
                e.printStackTrace();    
            }
        }
        Log.d("return size", totalSize + "");
        Toast.makeText(MainActivity.this, "Total cache size : " + totalSize, Toast.LENGTH_SHORT).show();
        return totalSize;
    }