问题
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);
}
}
}
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.
Any help would be higly appreciated.
回答1:
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);
}
});
回答2:
ApplicationInfo tmpInfo = context.getPackageManager().getApplicationInfo(packageName,-1);
long size = new File(tmpInfo.sourceDir).length();
来源:https://stackoverflow.com/questions/8801769/find-the-apk-size-of-installed-applications