Intent官方教程(4)用Intent构造应用选择框
Forcing an app chooser
When there is more than one app that responds to your implicit intent, the user can select which app to use and make that app the default choice for the action. This is nice when performing an action for which the user probably wants to use the same app from now on, such as when opening a web page (users often prefer just one web browser) .
However, if multiple apps can respond to the intent and the user might want to use a different app each time, you should explicitly show a chooser dialog. The chooser dialog asks the user to select which app to use for the action every time (the user cannot select a default app for the action). For example, when your app performs "share" with the ACTION_SEND action, users may want to share using a different app depending on their current situation, so you should always use the chooser dialog, as shown in figure 2.
Figure 2. A chooser dialog.
To show the chooser, create an Intent using createChooser() and pass it to startActivity(). For example:
1 Intent sendIntent = new Intent(Intent.ACTION_SEND); 2 ... 3 4 // Always use string resources for UI text. 5 // This says something like "Share this photo with" 6 String title = getResources().getString(R.string.chooser_title); 7 // Create intent to show the chooser dialog 8 Intent chooser = Intent.createChooser(sendIntent, title); 9 10 // Verify the original intent will resolve to at least one activity 11 if (sendIntent.resolveActivity(getPackageManager()) != null) { 12 startActivity(chooser); 13 }
This displays a dialog with a list of apps that respond to the intent passed to the createChooser() method and uses the supplied text as the dialog title.
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
· 【杂谈】分布式事务——高大上的无用知识?
2015-09-25 Drawable(3)Color State List Resource