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.
I would use a combinaison a little things.
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.
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
.
You can do the following:
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_APP_EMAIL);
getActivity().startActivity(intent);
User contributions licensed under CC BY-SA 3.0