如何创建启动任何电子邮件应用程序的意图?
我在这里和其他地方找到了有关创建发送电子邮件意图的各种主题,这似乎非常简单。我正在寻找启动用户可能拥有的任何电子邮件客户端的意图。
这是我见过的用于发送电子邮件的代码(发布仅供参考,这不能满足我的需求,因为我不想发送新消息):
Intent i = new Intent(Intent.ACTION_SEND);
i.setType("text/plain");
i.putExtra(Intent.EXTRA_EMAIL , new String[]{"[email protected]"});
i.putExtra(Intent.EXTRA_SUBJECT, "Subject of the message");
i.putExtra(Intent.EXTRA_TEXT , "Body of the message");
这是我专门按包名称启动 Gmail 客户端的代码:
PackageManager pm = getPackageManager();
Intent intent = pm.getLaunchIntentForPackage("com.google.android.gm");
startActivity(intent);
上面的代码可以工作,但不灵活,因为用户可能不使用 Gmail,而是使用其他内置电子邮件应用程序或第 3 方电子邮件应用程序。我正在寻找一种意图,在这种情况下可以调出选择器,以便用户可以决定启动哪个应用程序来阅读电子邮件。
有谁知道如何做到这一点?
I have found various topics here and elsewhere on creating an intent for sending e-mail and that seems to be pretty straightforward. I'm looking for an intent to just launch any e-mail client the user might have.
Here is the code I've seen for sending an e-mail (posted just for reference, this doesn't serve my needs as I don't want to send a new message):
Intent i = new Intent(Intent.ACTION_SEND);
i.setType("text/plain");
i.putExtra(Intent.EXTRA_EMAIL , new String[]{"[email protected]"});
i.putExtra(Intent.EXTRA_SUBJECT, "Subject of the message");
i.putExtra(Intent.EXTRA_TEXT , "Body of the message");
Here is the code I put together for specifically launching the Gmail client by package name:
PackageManager pm = getPackageManager();
Intent intent = pm.getLaunchIntentForPackage("com.google.android.gm");
startActivity(intent);
The code above works but isn't flexible in that a user might not be using Gmail but the other built-in e-mail application or a 3rd party e-mail app. I'm looking for an intent that would bring up the chooser in this case so the user can decide which app to launch to read e-mail.
Does anyone know how to accomplish this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
能否以某种方式使用 mailto URL 来实现此目的?
- 编辑 -
我能够使用以下代码示例来完成此任务:
Can a mailto URL be used in some fashion to accomplish this?
--Edit--
I was able to accomplish this using the following code sample:
另一种方法可能是 Intent.createChooser();并让用户
选择正确的应用程序。
顺便说一句,该列表不仅可以包含电子邮件应用程序
Another approach could be Intent.createChooser(); and let the user
to choose the right application.
BTW The list could contain not only email applications
不存在这样的
Intent
——您可以通过检查电子邮件应用程序的清单来判断这一点。您唯一能做的就是为自己建立一个您希望链接到的电子邮件客户端列表,并为每个客户端使用上面显示的
PackageManager
代码。There is no such
Intent
-- you can tell this by examining the manifest for the Email application.The only thing you can do is build yourself a list of email clients you wish to link to and use the
PackageManager
code you show above for each.