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

发布于 2024-12-16 19:40:07 字数 2304 浏览 0 评论 0原文

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

我正在寻找一种方法获取应用程序列表(用户安装或可更新,例如 Gmail、GMaps 等...)并将其放入 AlertDialog 中,类似于向主屏幕添加快捷方式(长按 -> 应用程序)。

这是我正在使用的线程,它具有获取列表的代码我需要的应用程序。但是我如何将其变成 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();
}

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

I'm currently developing an app that will allow the user to choose an app and launch it at a later time (there is more functionality but this is the main thing I'm having an issue with.)

I'm looking for a way to get a list of the applications (user installed or updateable ex. Gmail, GMaps, etc...) And throw it into an AlertDialog similar to how you add a shortcut to the Homescreen (Long press -> Applications).

This is the thread I'm using that has the code to get the list of applications that I need. However how would I turn this into an AlertDialog?

Here is the code from the thread.

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()

And here is the code I use for displaying an AlertDialog similar to what I want to use.

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

Any help as to getting this to display properly would be awesome.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不弃不离 2024-12-23 19:40:07

为什么不直接使用标准意图选择器? (请参阅这个)。否则,您可能想要解释什么没有按照您想要的方式显示,以及您真正希望它如何详细显示。

Why not just use the standard intent chooser? (See this). Otherwise, you probably want to explain what is not displaying the way you want, and how you really want it to look in detail.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文