如何从android代码打开默认邮件收件箱?
我正在尝试将按钮链接到邮件应用程序。不是发送邮件,只是打开收件箱。
我应该使用 Intent Intent = new Intent(...)
来执行此操作吗?
如果是这样,( )
之间应该是什么?
I'm trying to link a button to the mail app. Not to send mail, but just to open the inbox.
Should I do this with Intent intent = new Intent(...)
?
If so, what should be between the ( )
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(16)
2024 版
用法:
context.openDefaultEmailApp()
使用
Context
的 Kotlin 扩展函数,该函数也适用于Application
因为Application
是一个Context
2024 edition
Usage:
context.openDefaultEmailApp()
Using a Kotlin extension function of
Context
which also works withApplication
becauseApplication
is aContext
您可以使用以下命令打开 Android 默认电子邮件客户端:
You can open Android default e-mail client using this:
不幸的是,它看起来并不乐观。之前已经问过
如何启动电子邮件客户端直接进入收件箱视图?
您可以在撰写模式下打开电子邮件客户端,但您似乎已经知道这一点。
Unfortunately it doesn't look promising. This has been asked before
How do I launch the email client directly to inbox view?
you can open the email client in compose mode, but you seem to already know that.
有点晚了,这里是正确的工作代码。
有关更多详细信息,请查看此文档:
Bit late, here is proper working code.
For further details check this document:
对于 kotlin:
参考:https:// developer.android.com/reference/android/content/Intent.html#CATEGORY_APP_EMAIL
For kotlin:
Ref: https://developer.android.com/reference/android/content/Intent.html#CATEGORY_APP_EMAIL
该代码对我有用:
The code works for me:
如果没有附件,您可以简单地使用以下代码:
有关详细信息,我建议访问:
https://developer.android.com/guide/components/intents- common.html#电子邮件
You can simply use below code when for no attachment:
For details I recommend to visit:
https://developer.android.com/guide/components/intents-common.html#Email
我使用 jetpack compose,这对我有用:
I'm with jetpack compose and this works for me :
要打开它的新任务,请使用以下代码:
To open it new task use the below code:
如果设备中未配置默认邮件,有什么建议可以避免崩溃吗?
是的,可以打开 Android 默认电子邮件收件箱。
使用此代码:
此代码有效,您必须首先配置 Android 设备默认邮件。如果您已经配置了邮件,它就可以正常工作。否则,它会强制关闭并出现 NullPointerException 异常。
Any suggestions to avoid the crash if the default mail in the device is not configured?
Yes, it's possible to open the Android default email inbox.
Use this code:
This code works, you have to configure your Android device default mail first. If you already configured your mail it works fine. Otherwise, it force closes with a
NullPointerException
.根据答案 https://stackoverflow.com/a/28190156/3289338
从 Android 11 开始,系统不会返回 queryIntentActivities 的任何内容,因为我们首先需要在查询中(在清单中)添加一个条目,如下所示
,这里是解决方案的 kotlin 版本:
Based on the answer https://stackoverflow.com/a/28190156/3289338
Starting from Android 11 the system won't return anything for queryIntentActivities because we first need to add an entry in the queries (in the manifest) like this
and here a kotlin version of the solution:
这段代码对我有用。它会打开一个选择器,其中包含注册到设备的所有电子邮件应用程序并直接发送到收件箱:
This code worked for me. It opens a picker with all email apps registered to device and straight to Inbox:
如果目标是打开默认电子邮件应用程序以查看收件箱,那么关键是添加意图类别并使用 ACTION_MAIN 意图,如下所示:
https://developer.android.com/reference/android/content/Intent.html#CATEGORY_APP_EMAIL
If the goal is to open the default email app to view the inbox, then key is to add an intent category and use the ACTION_MAIN intent like so:
https://developer.android.com/reference/android/content/Intent.html#CATEGORY_APP_EMAIL