简体   繁体   中英

How can I get a list of all installed apps?

Help! All the same the black screen. Show as it is necessary to add a code. Thanks. I use such code:

package com.tipfile;

import java.util.ArrayList;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;


public class dop extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.dop);
    PackageManager pm = this.getPackageManager();

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

    ArrayList<ResolveInfo> list = (ArrayList<ResolveInfo>) 
    pm.queryIntentActivities(intent, PackageManager.PERMISSION_GRANTED);
    for (ResolveInfo rInfo : list) {
    System.out.println("Installed Applications " + rInfo.activityInfo.applicationInfo.loadLabel(pm).toString());
    }
    ListView listView = (ListView) findViewById(R.id.list);
    ArrayAdapter<String> aa = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1);
    listView.setAdapter(aa);
}}
  ArrayAdapter<String> aa = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1);
    listView.setAdapter(aa);

You dont initialize aa with any data hence the black screen.

You can build a List<String> variable from ArrayList<ResolveInfo> list = (ArrayList<ResolveInfo>) and use the ArrayAdapter constructor that accepts a list to fill aa

EDIT

List<String> myList = new ArrayList<String>();
ArrayList<ResolveInfo> list = (ArrayList<ResolveInfo>) pm.queryIntentActivities(intent, PackageManager.PERMISSION_GRANTED);
for (ResolveInfo rInfo : list) {
    System.out.println("Installed Applications " + rInfo.activityInfo.applicationInfo.loadLabel(pm).toString());
myList.add(rInfo.activityInfo.applicationInfo.loadLabel(pm).toString());
}

ListView listView = (ListView) findViewById(R.id.list);
ArrayAdapter<String> aa = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, myList);
listView.setAdapter(aa);
ArrayAdapter<String> aa = new
ArrayAdapter<String>(this,android.R.layout.simple_list_item_1);

Change these lines into

ArrayAdapter<String> aa = new
ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,list);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM