0

I would like to know if it is possible to have users open an email app of their choice without sending an email.

val intent = Intent(Intent.ACTION_SEND)
intent.type = "plain/text"
startActivity(Intent.createChooser(intent, "Choose Email"))

This allows the user to choose, but it opens the compose. I want just to open the email app.

1 Answers1

1

This is a bit tricky, but not impossible.

  1. Find all email clients
val resolveIntent = Intent(Intent.ACTION_SENDTO)
resolveIntent.setData(Uri.parse("mailto:default@recipient.com"))
val resolveInfoList = packageManager.queryIntentActivities(resolveIntent, PackageManager.MATCH_DEFAULT_ONLY)
  1. Start email client
val intents = resolveInfoList.mapNotNull { info -> packageManager.getLaunchIntentForPackage(info.activityInfo.packageName) }.toMutableList()
if(intents.isEmpty()) {
    //no mail client installed. Prompt user or throw exception
} else if(intents.size == 1) {
    //one mail client installed, start that
    startActivity(intents.first())
} else {
    //multiple mail clients installed, let user choose which one to start
    val chooser = Intent(Intent.ACTION_CHOOSER)
    chooser.putExtra(Intent.EXTRA_INTENT, intents.removeAt(0))
    chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, intents.toTypedArray())
    chooser.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
    startActivity(chooser)
}
F43nd1r
  • 7,690
  • 3
  • 24
  • 62
  • That seems like it is exactly what the OP does not want. I would expect that to open the email-compose screen. The OP seems to want opening something else, presumably whatever the launcher activity is. Using `ACTION_SENDTO` as a selector for a `MAIN`/`LAUNCHER` `Intent` would work. – CommonsWare Jan 07 '21 at 00:42
  • @CommonsWare check again - `SEND_TO` is only used to resolve packages, the launched intent is `packageManager.getLaunchIntentForPackage` – F43nd1r Jan 07 '21 at 00:43
  • @F43nd1r that worked and what I was looking for. However, I tried on Android API 30, and it doesn't work there, but it does work for < 30. Any idea? I would try to look into it. – Ronald Barrera Jan 08 '21 at 17:42