How to display a list of applications already installed on an Android phone in a list?

Ok, I use this code to get a list of applications on the phone, but how do I display them on the screen?

final PackageManager pm = getPackageManager();

    List<applicationinfo> packages = pm
            .getInstalledApplications(PackageManager.GET_META_DATA);

    for (ApplicationInfo packageInfo : packages) {

        Log.d(TAG, "Installed package :" + packageInfo.packageName);
        Log.d(TAG,
                "Launch Activity :"
                        + pm.getLaunchIntentForPackage(packageInfo.packageName));

    }

I am trying to display them in a list, but I have some problems. Can anybody help?

+3
source share
2 answers

This can help you.

public class AppList extends Activity {
 private ListView lView;
 private ArrayList results = new ArrayList();

 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
  lView = (ListView) findViewById(R.id.list1);
  PackageManager pm = this.getPackageManager();

  Intent intent = new Intent(Intent.ACTION_MAIN, null);
  intent.addCategory(Intent.CATEGORY_LAUNCHER);

  List<ResolveInfo> list = pm.queryIntentActivities(intent, PackageManager.PERMISSION_GRANTED);
  for (ResolveInfo rInfo : list) {
   results.add(rInfo.activityInfo.applicationInfo.loadLabel(pm).toString());
   Log.w("Installed Applications", rInfo.activityInfo.applicationInfo.loadLabel(pm).toString());
  } 
  lView.setAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, results));
    }
}
+7
source

You should create an adapter that implements the ListAdapter. You can use BaseAdapter as a basis. You can visit this tutorial for more information.

0
source

All Articles