本文介绍了查找已安装的应用程序的APK大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想计算的已安装应用程序的apk文件的大小。这里是code

I am trying to calculate apk file size of the installed applications. Here is the code

 public class PackageTabActivity extends ListActivity{

  private static final String APP_NAME = "app_name";
  private static final String APP_ICON = "app_icon";
  private static final String APP_SIZE = "app_size";
  Method getPackageSizeInfo = null;
  PackageManager pm  ;
@Override
public void onCreate(Bundle icicle){
    super.onCreate(icicle);
    pm = getPackageManager();


    try {
        getPackageSizeInfo = pm.getClass().getMethod("getPackageSizeInfo", String.class, IPackageStatsObserver.class);
    } catch (SecurityException e1) {

        e1.printStackTrace();
    } catch (NoSuchMethodException e1) {

        e1.printStackTrace();
    }
    new AppDetails().execute();
}

private boolean isSystemPackage(PackageInfo pkgInfo) {
    return ((pkgInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0) ? true
            : false;
}


class AppDetails extends AsyncTask<Void, Void, ArrayList<HashMap<String , Object>>>
{
     private  long   app_apk_size;
     ProgressDialog mDialog;
     @Override
     public void onPreExecute()
     {
             mDialog = new ProgressDialog(PackageTabActivity.this);
             mDialog.setCancelable(true);
             mDialog.setMessage("Loading... " );
             mDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
             mDialog.setProgress(0);
             mDialog.show();
     }
    @Override
    protected ArrayList<HashMap<String, Object>> doInBackground(
            Void... params) {

        ArrayList<HashMap<String,Object>> applistwithicon = new ArrayList<HashMap<String,Object>>();
        HashMap<String,Object> hm;
        String appname;
        Drawable icon = null;
        List<PackageInfo> packs = getPackageManager().getInstalledPackages(0);

        for(int i=0;i<packs.size();i++) {

            PackageInfo p = packs.get(i);


            if(!isSystemPackage(p))
            {
                hm = new HashMap<String, Object>();
                appname = p.applicationInfo.loadLabel(getPackageManager()).toString();

                icon = p.applicationInfo.loadIcon(getPackageManager());
                Log.v("package name", "package name =" + p.packageName);
                try {
                  getPackageSizeInfo.invoke(pm,  p.packageName, new IPackageStatsObserver.Stub()  {

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

                                   app_apk_size = pStats.codeSize;
                                   Log.v("apksize", "appsize = "+app_apk_size);
                    }
            });
                }catch(IllegalAccessException e) {}
                catch(IllegalArgumentException r) {}
                catch(InvocationTargetException w){}

            hm.put(APP_SIZE, (app_apk_size/(1024)));
            hm.put(APP_ICON, icon);
            hm.put(APP_NAME, appname);
            applistwithicon.add(hm);
            }
        }

        return applistwithicon;
    }
    @Override
    public void onPostExecute(ArrayList<HashMap<String, Object>> result)
    {
        mDialog.dismiss();
         CustomAdapterList myAdapter = new CustomAdapterList(result,PackageTabActivity.this );

          setListAdapter(myAdapter);

    }
}
}

下面IPackageStatsObserver是一个接口(AIDL文件)。这给了我在模拟器但电话正确的结果。它提供了对与应用尺寸不合适的混合物。

Here IPackageStatsObserver is an interface (aidl file).This gives me correct result on emulator but on phone. It gives mixture of right and wrong size of application.

我现在完全被卡住,无法找到任何解决办法。

I am now totally stuck and can't find any workaround.

任何帮助将是higly AP preciated。

Any help would be higly appreciated.

推荐答案

下面是约瑟夫一个黑客工具,为我工作。他原来的职位,可以发现这里

Here is a hack from Joseph that worked for me. His original post can be found here

PackageManager pm = getPackageManager();

Method getPackageSizeInfo = pm.getClass().getMethod(
    "getPackageSizeInfo", String.class, IPackageStatsObserver.class);

getPackageSizeInfo.invoke(pm, "com.android.mms",
    new IPackageStatsObserver.Stub() {

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

            Log.i(TAG, "codeSize: " + pStats.codeSize);
        }
    });

这篇关于查找已安装的应用程序的APK大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!