Shoaib Kakal
Shoaib Kakal

Reputation: 1336

Open Email App only through intent in Android Studio

I don't want to send emails to other users. I only want to open the launching activity of Email. I have tried it through different ways but every time send email(compose) is opening. But i want to open Email app only nothing else(Sent, outbox, spam, Trash etc).

My code is below

Intent intent = new Intent(Intent.ACTION_SENDTO)
    .setData(Uri.parse("mailto:"));

//check if the target app is available or not

if (intent.resolveActivity(getPackageManager()) != null) {
    startActivity(intent);
}

Again, This code opens the send email to(compose) option. But i want to open Email app only nothing else(Sent, outbox, spam, Trash etc).

Upvotes: 9

Views: 9503

Answers (2)

Slav
Slav

Reputation: 891

Building upon the answer by Vishal Nagvadiya, you should use makeMainSelectorActivity instead of starting the activity directly:

try {
    val intent = Intent
        .makeMainSelectorActivity(
            Intent.ACTION_MAIN,
            Intent.CATEGORY_APP_EMAIL
        )
        .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
    context.startActivity(intent)
} (ignored: ActivityNotFoundException) {
    // No activity found for the intent
}

In the JavaDocs of CATEGORY_APP_EMAIL, it is stated:

NOTE: This should not be used as the primary key of an Intent, since it will not result in the app launching with the correct action and category. Instead, use this with makeMainSelectorActivity(String, String) to generate a main Intent with this category in the selector.

Upvotes: 0

Vishal Nagvadiya
Vishal Nagvadiya

Reputation: 1278

Use this code to open default Mail Application :

try {
    Intent intent = new Intent(Intent.ACTION_MAIN);
    intent.addCategory(Intent.CATEGORY_APP_EMAIL);
    this.startActivity(intent);
} catch (android.content.ActivityNotFoundException e) {
    Toast.makeText(Dashboard.this, "There is no email client installed.", Toast.LENGTH_SHORT).show();
}

Upvotes: 18

Related Questions