android:ListView使用了很多“缓存的后台进程”

时间:2014-02-05 04:36:56

标签: android listview memory-management

我一直试图搜索这个,我知道有这么多问题,但我仍然无法找到我正在搜索的内容

我只是尝试使用AsyncTask和ArrayAdapter

显示所有已安装的应用程序

MyAdapter:

InstalledAdapter.java

public class InstalledAdapter extends ArrayAdapter<ResolveInfo> {
private List<ResolveInfo> applist = null;
private Context context;
private PackageManager packageManager;

public InstalledAdapter(Context context, int textViewResourceId,
        List<ResolveInfo> applist) {
    super(context, textViewResourceId, applist);
    this.context = context;
    this.applist = applist;
    packageManager = context.getPackageManager();
}

static class ViewHolder {
    TextView appname;
    ImageView appicon;
}

@Override
public int getCount() {
    return ((null != applist) ? applist.size() : 0);
}

@Override
public ResolveInfo getItem(int position) {
    return ((null != applist) ? applist.get(position) : null);
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View view = convertView;
    ViewHolder holder = null;
    if (null == view) {
        LayoutInflater layoutInflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = layoutInflater.inflate(R.layout.installed_base, null);
        holder = new ViewHolder();
        holder.appname = (TextView) view.findViewById(R.id.installed_name);
        holder.appicon = (ImageView) view.findViewById(R.id.installed_icon);
        view.setTag(holder);
    }
    else {
        holder = (ViewHolder) view.getTag();
    }

    ResolveInfo data = applist.get(position);
    holder.appname.setText(data.loadLabel(packageManager));
    holder.appicon.setImageDrawable(data.loadIcon(packageManager));

    return view;
}
}

MyActivity:

Test.java

public class Test extends Activity {

ListView installed_pkg;
private PackageManager packageManager = null;
private List<ResolveInfo> applist = null;
private InstalledAdapter pkgadapter = null;


Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.test);

    installed_pkg = (ListView) findViewById (R.id.installed_pkg);

    new LoadApplications().execute();

}

private class LoadApplications extends AsyncTask<Void, Void, Void> {
    private ProgressDialog progress = null;

    @Override
    protected Void doInBackground(Void... params) {
        Intent allpackages = new Intent(Intent.ACTION_MAIN);
        allpackages.addCategory(Intent.CATEGORY_LAUNCHER);
        applist = packageManager.queryIntentActivities(allpackages, PackageManager.GET_RESOLVED_FILTER);
        Collections.sort(applist, new ResolveInfo.DisplayNameComparator(packageManager));
        pkgadapter = new InstalledAdapter(Test.this,
                R.layout.installed_base, applist);

        return null;
    }

    @Override
    protected void onCancelled() {
        super.onCancelled();
    }

    @Override
    protected void onPostExecute(Void result) {
        installed_pkg.setAdapter(pkgadapter);
        progress.dismiss();
        super.onPostExecute(result);
    }

    @Override
    protected void onPreExecute() {
        loading = getResources().getString(R.string.loading);
        progress = ProgressDialog.show(Test.this, null,
                loading);
        super.onPreExecute();

    }

    @Override
    protected void onProgressUpdate(Void... values) {
        super.onProgressUpdate(values);
    }
}
}

我在我的设备上安装了

进行了测试

主要问题

从上到下滚动(到达结束)后,我在设置

上检查了“缓存后台进程”

这会占用大量内存,使用122 MB缓存后台进程显示

我的问题

这是正常的吗?

知道如何减少内存消耗吗?

0 个答案:

没有答案