Using Android Intent.ACTION_SEND for sending email

2019-01-08 17:37发布

I'm using Intent.ACTION_SEND to send an email. However, when I call the intent it is showing choices to send a message, send an email, and also to send via bluetooth. I want it to only show choices to send an email. How can I do this?

13条回答
三岁会撩人
2楼-- · 2019-01-08 18:04

This is a combination of Jack Dsilva and Jignesh Mayani solutions:

    try
    {
        Intent gmailIntent = new Intent(Intent.ACTION_SEND);
        gmailIntent.setType("text/html");

        final PackageManager pm = _activity.getPackageManager();
        final List<ResolveInfo> matches = pm.queryIntentActivities(gmailIntent, 0);
        String gmailActivityClass = null;

        for (final ResolveInfo info : matches)
        {
            if (info.activityInfo.packageName.equals("com.google.android.gm"))
            {
                gmailActivityClass = info.activityInfo.name;

                if (gmailActivityClass != null && !gmailActivityClass.isEmpty())
                {
                    break;
                }
            }
        }

        gmailIntent.setClassName("com.google.android.gm", gmailActivityClass);
        gmailIntent.putExtra(Intent.EXTRA_EMAIL, new String[] { "yourmail@gmail.com" });
        gmailIntent.putExtra(Intent.EXTRA_SUBJECT, "Subject");
        gmailIntent.putExtra(Intent.EXTRA_CC, "cc@gmail.com"); // if necessary
        gmailIntent.putExtra(Intent.EXTRA_TEXT, "Email message");
        gmailIntent.setData(Uri.parse("yourmail@gmail.com"));
        this._activity.startActivity(gmailIntent);
    }

    catch (Exception e)
    {
        Intent i = new Intent(Intent.ACTION_SEND);
        i.putExtra(Intent.EXTRA_EMAIL, new String[] { "yourmail@gmail.com" });
        i.putExtra(Intent.EXTRA_SUBJECT, "Subject");
        i.putExtra(Intent.EXTRA_CC, "cc@gmail.com"); // if necessary
        i.putExtra(Intent.EXTRA_TEXT, "Email message");
        i.setType("plain/text");
        this._activity.startActivity(i);
    }

So, at first it will try to open gmail app and in case a user doesn't have it then the second approach will be implemented.

查看更多
登录 后发表回答