我尝试编写一个简单的方法,该方法显示一个对话框,其中包含所有已安装应用程序的列表。当我单击其中一个项目时,应返回带有packagename的String。
我试过跟随,但显然它不起作用。
public static String createAppPicker(Context context) {
final String packageName = "";
Dialog dialog = new Dialog(context);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
final ListView appList;
final PackageManager pm = context.getPackageManager();
final List<ApplicationInfo> packages = pm.getInstalledApplications(PackageManager.GET_META_DATA);
final ArrayList<String> titles = new ArrayList<String>();
for(int i = 0; i < packages.size(); i++) {
titles.add(packages.get(i).loadLabel(pm).toString());
}
Collections.sort(titles);
final String[] values = new String[packages.size()];
for(int i = 0; i < titles.size(); i++) {
values[i] = titles.get(i);
}
dialog.setContentView(R.layout.dialog_app_picker);
appList = (ListView)dialog.findViewById(R.id.dialog_app_picker_listview);
appList.setAdapter(new ArrayAdapter<String>(context, android.R.layout.simple_list_item_1, values));
appList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
**packageName = packages.get(position).packageName;**
}
});
dialog.show();
return packageName;
}
有人有另一种解决方案吗?