public class PackageTabActivity extends ListActivity{
HashMap<String,Object> hm ;
ArrayList<HashMap<String,Object>> applistwithicon ;
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();
applistwithicon = new ArrayList<HashMap<String,Object>>();
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>> app = new ArrayList<HashMap<String,Object>>();
List<PackageInfo> packs = getPackageManager().getInstalledPackages(0);
long [] size = new long[packs.size()];
for(int i=0;i<packs.size();i++) {
PackageInfo p = packs.get(i);
if(!isSystemPackage(p))
{
/* String package_name = p.applicationInfo.packageName;
appname[i] = p.applicationInfo.loadLabel(getPackageManager()).toString();
icon[i] = p.applicationInfo.loadIcon(getPackageManager());
size[i] = GetSize(package_name);
// Log.v("Test", "app_name = " +appname[i]+" app_size = "+size[i]);*/
app= GetSize(p);
}
}
//
return app;
}
private ArrayList<HashMap<String, Object>> GetSize(final PackageInfo pInfo)
{
try
{
getPackageSizeInfo.invoke(pm, pInfo.applicationInfo.packageName, new IPackageStatsObserver.Stub()
{
public void onGetStatsCompleted(PackageStats pStats, boolean succeeded) throws RemoteException
{
app_apk_size= pStats.codeSize/1024;
if(app_apk_size!=0)
{
// If I uncomment-out the below commented-out lines, I get
// an incorrect result:
// hm = new HashMap<String, Object>();
// hm.put(APP_SIZE, app_apk_size);
// hm.put(APP_NAME, pInfo.applicationInfo.loadLabel(pm));
Log.v("apksize", "appsize = "+app_apk_size+"appname = "+pInfo.applicationInfo.loadLabel(pm));
// applistwithicon.add(hm);
}
}
});
}
catch(IllegalAccessException e) {}
catch(IllegalArgumentException r) {}
catch(InvocationTargetException w){}
return applistwithicon;
}
@Override
public void onPostExecute(ArrayList<HashMap<String, Object>> result)
{
mDialog.dismiss();
CustomAdapterList myAdapter = new CustomAdapterList(result,PackageTabActivity.this );
setListAdapter(myAdapter);
}
}
}
但如果我取消注释掉已注释掉的行,则会记录错误的结果。
有谁知道为什么?
答案 0 :(得分:1)
我不知道pInfo.applicationInfo.loadLabel(pm)
做了什么,但尝试将结果放入var而不是调用方法两次。