首先创建 main.xml 文件如下,
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/list1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
这list
将用于填充已安装的应用程序,
现在创建 Activity 类来获取已安装的应用程序,
public class AppList extends Activity {
private ListView lView;
private ArrayList results;
List<ResolveInfo> list;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
results = new ArrayList();
lView = (ListView) findViewById(R.id.list1);
PackageManager pm = this.getPackageManager();
Intent intent = new Intent(Intent.ACTION_MAIN, null);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
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));
}
}
编辑 -如果您想从列表中打开应用程序,请单击它。您必须覆盖 ListView 的 onItemClick 方法,并且在该方法中,您必须传递打开所选应用程序的意图。
看这里,
这些很可能会解决您的问题。