Start Gmail / Email Intent in Android showing list of emails

廉价感情. 提交于 2021-01-29 14:02:18

问题


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"))
    }
}

This implementation will open gmail, but in compose email screen. How do I manage to show the list of emails in the inbox instead?


回答1:


I want to open the phone's email app

That emailPackage value will try to open GMail, not the user's choice of email app.

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?

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>.

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:

  • Get rid of emailPackage and the code that uses it
  • Use Intent(Intent.ACTION_SENDTO, Uri.parse("mailto:")) as the selector for a MAIN/LAUNCHER Intent, to bring up a chooser (if needed) of email clients

See this answer for more on selectors, though in that case they are using it to refine an ACTION_SEND Intent.



来源:https://stackoverflow.com/questions/63632859/start-gmail-email-intent-in-android-showing-list-of-emails

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!