在Android中启动Gmail/电子邮件意图,显示电子邮件列表
生成邮件后,我已经实现了Firebase电子邮件链接登录,我想打开手机的电子邮件应用程序,以便用户可以直接打开已发送的邮件.
I've implemented the firebase email link login, after generating the mail I want to open the phone's email app, so the user can directly open the mail, which was sent.
fun startEmailApp(context: Context) {
val emailPackage = "com.google.android.gm"
val isGmailInstalled = isAppInstalled(context, emailPackage)
val intent = Intent(Intent.ACTION_SEND)
context.startActivity(Intent.createChooser(intent, "choose an email client"))
if (isGmailInstalled) {
intent.type = "text/html"
intent.setPackage(emailPackage)
context.startActivity(intent)
} else {
intent.type = "message/rfc822";
context.startActivity(Intent.createChooser(intent, "choose an email client"))
}
}
此实现将打开gmail,但在撰写电子邮件屏幕中.我该如何在收件箱中显示电子邮件列表?
This implementation will open gmail, but in compose email screen. How do I manage to show the list of emails in the inbox instead?
我要打开手机的电子邮件应用程序
I want to open the phone's email app
该 emailPackage
值将尝试打开GMail,而不是用户选择的电子邮件应用程序.
That emailPackage
value will try to open GMail, not the user's choice of email app.
顺便说一句,请注意,除非您添加< queries>
元素以将查找Gmail的功能列入白名单,否则您的 isAppInstalled()
可能会在Android 11上中断.我们现在无法通过 PackageManager
查找其他应用程序.
BTW, note that your isAppInstalled()
may break on Android 11, unless you add a <queries>
element to whitelist your ability to find Gmail. Our ability to find other apps via PackageManager
is now limited.
我该如何在收件箱中显示电子邮件列表?
How do I manage to show the list of emails in the inbox instead?
电子邮件应用程序无需将其作为导出的活动,更不用说具有文档化和受支持的< intent-filter>
的活动了.
There is no requirement for an email app to have that be an exported activity, let alone one that has a documented and supported <intent-filter>
.
鉴于您显然只想支持Gmail,您可以尝试打开其 ACTION_MAIN
/ CATEGORY_LAUNCHER
活动.对于发射器,将会有其中一种,并且可能用户可以轻松地从那里到达其收件箱.
Given that apparently you only want to support Gmail, you could try opening their ACTION_MAIN
/CATEGORY_LAUNCHER
activity. There is going to be one of those, for launchers, and probably the user can easily get to their inbox from there.
如果您想支持更多的电子邮件应用程序,则可以:
If you wanted to support a wider range of email apps, you could:
- 摆脱
emailPackage
和使用它的代码 - 使用
Intent(Intent.ACTION_SENDTO,Uri.parse("mailto:"))
作为MAIN
/LAUNCHER
的选择器>Intent
,以显示电子邮件客户端的选择器(如果需要)
- Get rid of
emailPackage
and the code that uses it - Use
Intent(Intent.ACTION_SENDTO, Uri.parse("mailto:"))
as the selector for aMAIN
/LAUNCHER
Intent
, to bring up a chooser (if needed) of email clients
有关选择器的更多信息,请参见此答案,尽管在这种情况下,他们正在使用它来完善 ACTION_SEND
意图
.
See this answer for more on selectors, though in that case they are using it to refine an ACTION_SEND
Intent
.