Created
May 7, 2020 19:00
-
-
Save MisterJimson/11462441ff3a9ee29cba4d16a004b41e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun emailAppIntent() { | |
val emailIntent = Intent(Intent.ACTION_VIEW, Uri.parse("mailto:")) | |
val packageManager = packageManager | |
val activitiesHandlingEmails = packageManager.queryIntentActivities(emailIntent, 0) | |
if (activitiesHandlingEmails.isNotEmpty()) { | |
// use the first email package to create the chooserIntent | |
val firstEmailPackageName = activitiesHandlingEmails.first().activityInfo.packageName | |
val firstEmailInboxIntent = packageManager.getLaunchIntentForPackage(firstEmailPackageName) | |
val emailAppChooserIntent = Intent.createChooser(firstEmailInboxIntent, "") | |
// created UI for other email packages and add them to the chooser | |
val emailInboxIntents = mutableListOf<LabeledIntent>() | |
for (i in 1 until activitiesHandlingEmails.size) { | |
val activityHandlingEmail = activitiesHandlingEmails[i] | |
val packageName = activityHandlingEmail.activityInfo.packageName | |
val intent = packageManager.getLaunchIntentForPackage(packageName) | |
emailInboxIntents.add( | |
LabeledIntent( | |
intent, | |
packageName, | |
activityHandlingEmail.loadLabel(packageManager), | |
activityHandlingEmail.icon | |
) | |
) | |
} | |
val extraEmailInboxIntents = emailInboxIntents.toTypedArray() | |
val finalIntent = emailAppChooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, extraEmailInboxIntents) | |
startActivity(finalIntent); | |
} else { | |
//todo | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment