How to open default email client in Android, without sending a message?

0

I'm looking for a way to open default email application on Android, but without using it to send a message. I know I can do this using mailto:// or intent params, but this automatically opens new message screen. What I want to to archieve, is just opening the app itself.

So far, I have tried

override fun startEmailApplication() {
    val intent = Intent(Intent.ACTION_MAIN)
    intent.addCategory(Intent.CATEGORY_APP_EMAIL)
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
    startActivity(intent)
}

But every time I get

android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW cat=[android.intent.category.APP_EMAIL] flg=0x10000000 }

Altrough an email app (AquaMail, Outlook) is installed.

java
android
email
android-intent
kotlin
asked on Stack Overflow Jan 15, 2019 by ikurek

2 Answers

0

I would use a combinaison a little things.

  1. Detect the responding packages for a given Intent:

    Intent intent = new Intent(Intent.ACTION_SENDTO);
    intent.setType("text/plain");
    List<ResolveInfo> resolveInfos = getPackageManager().queryIntentActivities(intent, 0);
    boolean canResolve = resolveInfos.size() > 0; 
    

This will list the available packages to respond to such Intent. Using queryIntentActivities() allows me to retrieve ResolveInfo which exposes more information about the app.

  1. Select the first one and open it using its packageName:

    if (resolveInfos.size() > 0) {
        startActivity(getPackageManager().getLaunchIntentForPackage(resolveInfos.get(0).resolvePackageName))
    }
    

You also won't have the ActivityNotFoundException because we check before hand that something will respond to our Intent. Feel free to handle the failing case in a else.

answered on Stack Overflow Jan 15, 2019 by shkschneider
-1

You can do the following:

Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_APP_EMAIL);
getActivity().startActivity(intent);
answered on Stack Overflow Jan 15, 2019 by Benjamin Kusicky

User contributions licensed under CC BY-SA 3.0