以下程式碼會查詢已安裝的Apps,並將結果顯示在ListView元件:
PackageManager pmPack;
pmPack = getPackageManager();
List packinfo = pmPack.getInstalledPackages(PackageManager.GET_ACTIVITIES);
String[] pak = new String[packinfo.size()];
for ( int i=0; i < packinfo.size(); i++) {
PackageInfo p = (PackageInfo) packinfo.get(i);
pak[i] = p.packageName;
}
ListView lv = (ListView) findViewById(R.id.listView1);
ArrayAdapter lstadapter = new ArrayAdapter(
MainActivity.this,
android.R.layout.simple_list_item_1, pak);
lv.setAdapter(lstadapter);