java - 获取并显示 Android 上已安装程序的列表

标签 java android list menu android-alertdialog

我目前正在开发一个应用程序,允许用户选择一个应用程序并稍后启动它(有更多功能,但这是我遇到的主要问题。)

我正在寻找一种方法来获取应用程序列表(用户安装的或可更新的,例如 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()

这是我用来显示类似于我想要使用的警报对话框的代码。

//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();
}

任何有关使其正确显示的帮助都会很棒。

最佳答案

为什么不直接使用标准 Intent 选择器? (参见this)。否则,您可能想要详细解释哪些内容没有按照您想要的方式显示,以及您真正希望它的外观如何。

关于java - 获取并显示 Android 上已安装程序的列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8158505/

相关文章:

java - 在内部从 Java 进行多个 REST 调用

java - Android studio - 努力从应用程序 Assets 共享图像/音频/视频文件

java - 如何创建一个包含比较元素以获得最高元素的数组的方法?

android - 在特定标签栏中打开应用程序

java - 比较两个列表时发生奇怪的情况(groovy)

java - 比较java中的两个列表

android - 在 xml 预览中排除布局

android - AudioManager setStreamVolume 没有标志

python - 如何合并这两段Python列表代码?

python - 有没有办法创建与用户输入一样多的列表?