Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to open the default mail inbox from android code?

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 ( )?

like image 323
Sander Swart Avatar asked Nov 11 '11 19:11

Sander Swart


4 Answers

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:

Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_APP_EMAIL);
getActivity().startActivity(intent);

https://developer.android.com/reference/android/content/Intent.html#CATEGORY_APP_EMAIL

like image 140
Ben Yee Avatar answered Nov 14 '22 12:11

Ben Yee


This code worked for me. It opens a picker with all email apps registered to device and straight to Inbox:

Intent emailIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("mailto:"));
    PackageManager pm = getPackageManager();

    List<ResolveInfo> resInfo = pm.queryIntentActivities(emailIntent, 0);
    if (resInfo.size() > 0) {
        ResolveInfo ri = resInfo.get(0);
        // First create an intent with only the package name of the first registered email app
        // and build a picked based on it
        Intent intentChooser = pm.getLaunchIntentForPackage(ri.activityInfo.packageName);
        Intent openInChooser =
                Intent.createChooser(intentChooser,
                        getString(R.string.user_reg_email_client_chooser_title));

        // Then create a list of LabeledIntent for the rest of the registered email apps 
        List<LabeledIntent> intentList = new ArrayList<LabeledIntent>();
        for (int i = 1; i < resInfo.size(); i++) {
            // Extract the label and repackage it in a LabeledIntent
            ri = resInfo.get(i);
            String packageName = ri.activityInfo.packageName;
            Intent intent = pm.getLaunchIntentForPackage(packageName);
            intentList.add(new LabeledIntent(intent, packageName, ri.loadLabel(pm), ri.icon));
        }

        LabeledIntent[] extraIntents = intentList.toArray(new LabeledIntent[intentList.size()]);
        // Add the rest of the email apps to the picker selection
        openInChooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, extraIntents);
        startActivity(openInChooser);
    }
like image 26
Larisa Hogas Avatar answered Nov 14 '22 13:11

Larisa Hogas


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:

Intent intent = getPackageManager().getLaunchIntentForPackage("com.android.email");
startActivity(intent);


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.

like image 34
Mayur Bhola Avatar answered Nov 14 '22 13:11

Mayur Bhola


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

<manifest ...>

<queries>
    ...
    <intent>
      <action
        android:name="android.intent.action.VIEW" />
      <data
        android:scheme="mailto" />
    </intent>
</queries>

...

</manifest>

and here a kotlin version of the solution:

fun Context.openMailbox(chooserTitle: String) {
    val emailIntent = Intent(Intent.ACTION_VIEW, Uri.parse("mailto:"))

    val resInfo = packageManager.queryIntentActivities(emailIntent, 0)
    if (resInfo.isNotEmpty()) {
        // First create an intent with only the package name of the first registered email app
        // and build a picked based on it
        val intentChooser = packageManager.getLaunchIntentForPackage(
            resInfo.first().activityInfo.packageName
        )
        val openInChooser = Intent.createChooser(intentChooser, chooserTitle)

        // Then create a list of LabeledIntent for the rest of the registered email apps
        val emailApps = resInfo.toLabeledIntentArray(packageManager)

        // Add the rest of the email apps to the picker selection
        openInChooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, emailApps)
        startActivity(openInChooser)
    } else {
        Timber.e("No email app found")
    }
}

private fun List<ResolveInfo>.toLabeledIntentArray(packageManager: PackageManager): Array<LabeledIntent> = map {
    val packageName = it.activityInfo.packageName
    val intent = packageManager.getLaunchIntentForPackage(packageName)
    LabeledIntent(intent, packageName, it.loadLabel(packageManager), it.icon)
}.toTypedArray()
like image 28
Timo Avatar answered Nov 14 '22 14:11

Timo