如何创建启动任何电子邮件应用程序的意图?

发布于 2024-09-07 19:05:41 字数 893 浏览 4 评论 0原文

我在这里和其他地方找到了有关创建发送电子邮件意图的各种主题,这似乎非常简单。我正在寻找启动用户可能拥有的任何电子邮件客户端的意图。

这是我见过的用于发送电子邮件的代码(发布仅供参考,这不能满足我的需求,因为我不想发送新消息):

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 技术交流群。

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

发布评论

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

评论(3

终遇你 2024-09-14 19:05:41

能否以某种方式使用 mailto URL 来实现此目的?
- 编辑 -
我能够使用以下代码示例来完成此任务:

mt = MailTo.parse("mailto:[email protected]");
sendIntent = new Intent(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_EMAIL, new String[]{mt.getTo()});
sendIntent.putExtra(Intent.EXTRA_TEXT, "");
sendIntent.putExtra(Intent.EXTRA_SUBJECT, "Enter a subject");
sendIntent.setType("message/rfc822");
startActivity(Intent.createChooser(sendIntent, "Send a Message:"));

Can a mailto URL be used in some fashion to accomplish this?
--Edit--
I was able to accomplish this using the following code sample:

mt = MailTo.parse("mailto:[email protected]");
sendIntent = new Intent(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_EMAIL, new String[]{mt.getTo()});
sendIntent.putExtra(Intent.EXTRA_TEXT, "");
sendIntent.putExtra(Intent.EXTRA_SUBJECT, "Enter a subject");
sendIntent.setType("message/rfc822");
startActivity(Intent.createChooser(sendIntent, "Send a Message:"));
哽咽笑 2024-09-14 19:05:41

另一种方法可能是 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

不知所踪 2024-09-14 19:05:41

有谁知道如何实现
这个?

不存在这样的Intent——您可以通过检查电子邮件应用程序的清单来判断这一点。

您唯一能做的就是为自己建立一个您希望链接到的电子邮件客户端列表,并为每个客户端使用上面显示的 PackageManager 代码。

Does anyone know how to accomplish
this?

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.

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