找到已安装应用程序的APK大小

时间:2012-01-10 10:28:15

标签: java android filesize apk

我正在尝试计算已安装应用程序的apk文件大小。这是代码

 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文件)。 这给了我在模拟器上但在手机上的正确结果。它给出了正确和错误的应用程序大小的混合。

我现在完全陷入困境,无法找到任何解决方法。

任何帮助都会受到高度赞赏。

2 个答案:

答案 0 :(得分:2)

ApplicationInfo tmpInfo = context.getPackageManager().getApplicationInfo(packageName,-1);
long size = new File(tmpInfo.sourceDir).length();

答案 1 :(得分:0)

这是来自约瑟夫的黑客,对我有用。他的原始帖子可以找到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);
        }
    });