2011-03-22 61 views
3

好吧,我使用此代碼來獲取手機上的應用程序列表,但我如何在屏幕上顯示它們?如何顯示已安裝在Android手機上的應用程序列表中的列表?

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

    } 

我試着在列表視圖中顯示它們但有一點麻煩的IM。有人可以幫忙嗎?

+0

請參閱[本文](http://androidboss.com/getting-list-of-installed-applications-in-android/)。 – Mudassir 2011-03-22 10:57:44

回答

7

這可能對你有一些幫助。

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

此代碼正在工作...但我有一個查詢如何獲得圖標.. – 2012-09-22 08:00:59

0

您應該創建一個實現ListAdapter的Adapter。作爲基礎,您可以使用BaseAdapter。欲瞭解更多信息,你可以訪問this tutorial

相關問題