Using Android Intent.ACTION_SEND for sending email

前端 未结 17 1271
野趣味
野趣味 2020-11-29 23:07

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

17条回答
  •  借酒劲吻你
    2020-11-29 23:52

    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 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.

提交回复
热议问题