如何制作一个可定制的按钮来启动应用程序?
我正在尝试向我的应用程序添加一个按钮。单击时,我想启动一个选择对话框,显示所有快捷方式或已安装的应用程序。选择一个应该永久设置启动该应用程序的按钮。
我了解如何使用 packagemanager 获取已安装应用程序的列表:
PackageManager pm = getPackageManager();
List<ApplicationInfo> packages = pm.getInstalledApplications(PackageManager.GET_META_DATA);
但是我真的需要接受它并使用 ListAdapter 并从头开始创建一个单独的对话框吗?
我觉得我在其他应用程序中多次看到过这个选择菜单(例如当您添加快捷方式时的任何启动器应用程序,或者当您添加新快捷方式时在 Google 的 Car Home 应用程序中)。没有通用的方法来使用这个快捷选择菜单吗?
我已经搜索了所有这些论坛,但找不到其他的。任何帮助将不胜感激。谢谢。
I am trying to add a button to my application. When clicked I would like to launch a selection dialog that shows all shortcuts or installed applications. Choosing one should permanently set the button to launch that application.
I understand how to use packagemanager to get a list of installed applications:
PackageManager pm = getPackageManager();
List<ApplicationInfo> packages = pm.getInstalledApplications(PackageManager.GET_META_DATA);
but do I really need to take this and use a ListAdapter and create a seperate dialog from scratch?
I feel like I've seen this selection menu in other apps multiple times (such as any launcher app when you go to add a shortcut, or in Google's Car Home app when you add a new shortcut). Is there no stock way to use this shortcut selection menu?
I've searched these forums all over and can't find otherwise. Any help will be much appreciated. Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对于选择应用程序,是的。
该“快捷选择菜单”并不是选择应用程序。它正在选择一个活动,可能使用
ACTION_PICK_ACTIVITY
。For picking an application, yes.
That "shortcut selection menu" is not picking an application. It is picking an activity, probably using
ACTION_PICK_ACTIVITY
.对于那些感兴趣的人,我最终是如何完成它的:
当您创建 Intent mainIntent(在下面的代码中)并使用 ACTION_MAIN 和 addCategory CATEGORY_LAUNCHER 时,您可以将其添加为 pickIntent 的 EXTRA。这样做可以缩小选择器菜单的范围,仅显示已安装的应用程序。
下面是一些让简单的启动器按钮运行的代码:
For those interested, here is how I eventually accomplished it:
When you create the Intent mainIntent (in the code below) and use ACTION_MAIN and addCategory CATEGORY_LAUNCHER, you can add it as an EXTRA for pickIntent. Doing this narrows down the picker menu to show only installed applications.
Here is some code to get a simple launcher button going: