我目前正在开发一款应用程序,允许用户选择一个应用程序并在以后启动它(有更多功能,但这是我遇到的主要问题。)
我正在寻找一种方法来获取应用程序列表(用户已安装或可更新,例如Gmail,GMaps等等)并将其投入AlertDialog,类似于向主屏幕添加快捷方式(长按 - >应用程序)。
This是我正在使用的线程,它有代码来获取我需要的应用程序列表。但是我怎么把它变成AlertDialog?
以下是该主题的代码。
public void getApps()
{
PackageManager pm = getPackageManager();
List<ApplicationInfo> apps = pm.getInstalledApplications(0);
List<ApplicationInfo> installedApps = new ArrayList<ApplicationInfo>();
for(ApplicationInfo app : apps) {
//checks for flags; if flagged, check if updated system app
if((app.flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) == 1) {
installedApps.add(app);
//it's a system app, not interested
} else if ((app.flags & ApplicationInfo.FLAG_SYSTEM) == 1) {
//Discard this one
//in this case, it should be a user-installed app
} else {
installedApps.add(app);
}
}
}//end getApps()
这是我用来显示类似于我想要使用的AlertDialog的代码。
//PseudoCode does not compile
public void displayAppList(View v)
{
final CharSequence[] items = {getApps()};
AlertDialog.Builder builder = new AlertDialog.Builder(SchedulerActivity.this);
builder.setTitle("Choose an App To Launch");
builder.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
appChoiceString[count] = items[item];
Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show();
}
});
builder.setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Toast.makeText(SchedulerActivity.this, "Success", Toast.LENGTH_SHORT).show();
}
});
builder.setNegativeButton("No",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Toast.makeText(SchedulerActivity.this, "Fail", Toast.LENGTH_SHORT).show();
}
});
AlertDialog alert = builder.create();
alert.show();
}
任何有关正确显示的帮助都会很棒。