如何获取所有已安装应用的列表?

时间:2011-05-16 10:39:27

标签: android package-managers

帮助!黑屏一样。显示,因为有必要添加代码。谢谢。 我使用这样的代码:

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);
}}

2 个答案:

答案 0 :(得分:2)

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

你没有用任何数据初始化aa,因此黑屏。

您可以从List<String>构建ArrayList<ResolveInfo> list = (ArrayList<ResolveInfo>)变量,并使用接受列表的ArrayAdapter constructor来填充aa

修改

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);

答案 1 :(得分:0)

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

将这些行改为

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