Android Intent Chooser

3.6k views Asked by At

I am creating an app in which there is a Gmail button and when user when clicks on Gmail it sends a mail to the particular receipient using inbuilt gmail client but what happens is when the user clicks on gmail button ,it opens a list of all the clients installed in the device eg hike,drive,email,gmail,etc with users asking for the option.

Here is my code

       protected void sendEmail() {
        Log.i("Send email", "");
        String[] TO = {""};
        String[] CC = {""};
        Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
        emailIntent.setType("message/rfc822");   //should i have to change this line of code
        emailIntent.putExtra(Intent.EXTRA_EMAIL, TO);
        emailIntent.putExtra(Intent.EXTRA_CC, CC);
        emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Your subject");  //message subject
        emailIntent.putExtra(Intent.EXTRA_TEXT, "Email message goes here");   //message content

        try {
            startActivity(Intent.createChooser(emailIntent,"Send mail..."));
            finish();
            Log.i("Finished sending email.", "");
        }
        catch (android.content.ActivityNotFoundException ex) {
            Toast.makeText(MailSenderActivity.this, "There is no email client installed.", Toast.LENGTH_SHORT).show();
        }
    }

What I want is when user clicks on the Gmail button it only opens inbuilt Gmail and Email clients only not hike and all. How can I acheive this (by changing the type or what?). I have tried so many ways but in vain.

2

There are 2 answers

2
Pranav Darji On

You can use the below code to redirect to Gmail Without asking for other email application

final Intent intent = new Intent(Intent.ACTION_SEND);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        intent.setType("plain/text");

        PackageInfo pInfo = null;
        try {
            pInfo = getPackageManager().getPackageInfo(getPackageName(), 0);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        String version = "";
        if (pInfo != null) {
            version = pInfo.versionName;
        }

        final PackageManager pm = getPackageManager();
        final List<ResolveInfo> matches = pm.queryIntentActivities(intent, 0);
        ResolveInfo best = null;
        for (final ResolveInfo info : matches)
            if (info.activityInfo.packageName.endsWith(".gm") ||
                    info.activityInfo.name.toLowerCase().contains("gmail")) best = info;
        if (best != null)
            intent.setClassName(best.activityInfo.packageName, best.activityInfo.name);

        intent.putExtra(Intent.EXTRA_EMAIL, "abc.gmail.com");
        intent.putExtra(Intent.EXTRA_SUBJECT,"subject here !");
        intent.putExtra(Intent.EXTRA_TEXT,"Body here !");
        startActivity(intent);
17
Sanket Shah On

Here is my code

PackageManager pm = getPackageManager();
Intent tempIntent = new Intent(Intent.ACTION_SEND);
tempIntent.setType("*/*");
List<ResolveInfo> resInfo = pm.queryIntentActivities(tempIntent, 0);
for (int i = 0; i < resInfo.size(); i++) {
    ResolveInfo ri = resInfo.get(i);
    if (ri.activityInfo.packageName.contains("android.gm")) {
        myIntent.setComponent(new ComponentName(ri.activityInfo.packageName, ri.activityInfo.name));
        myIntent.setAction(Intent.ACTION_SEND);
        myIntent.putExtra(Intent.EXTRA_EMAIL, new String[]{"[email protected]"});
        myIntent.setType("message/rfc822");
        myIntent.putExtra(Intent.EXTRA_TEXT, "extra text");
        myIntent.putExtra(Intent.EXTRA_SUBJECT, "Extra subject");
        myIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("uri://your/uri/string");
    }
}
startActivity(myIntent);

OR You can do with below code

Intent intent = new Intent(Intent.ACTION_SEND);

String[] strTo = { getString(R.string.mailto) };

intent.putExtra(Intent.EXTRA_EMAIL, strTo);
intent.putExtra(Intent.EXTRA_SUBJECT, getString(R.string.mail_subject));
intent.putExtra(Intent.EXTRA_TEXT, getString(R.string.mail_body));

Uri attachments = Uri.parse(image_path);
intent.putExtra(Intent.EXTRA_STREAM, attachments);

intent.setType("message/rfc822");

intent.setPackage("com.google.android.gm");

startActivity(intent);