How to open Gmail Compose when a button is clicked in Android App?

后端 未结 11 996
别跟我提以往
别跟我提以往 2020-12-01 06:25

I am trying to open up Gmail Compose screen when a button is clicked in my Android App. Do I need some API key for this from Google? or what do I need to do in my button onC

相关标签:
11条回答
  • 2020-12-01 06:56
    public static void openGmail(Activity activity,String[] email, String subject, String content) {
        Intent emailIntent = new Intent(Intent.ACTION_SEND);
        emailIntent.putExtra(Intent.EXTRA_EMAIL, email);
        emailIntent.putExtra(Intent.EXTRA_SUBJECT, subject);
        emailIntent.setType("text/plain");
        emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, content);
        final PackageManager pm = activity.getPackageManager();
        final List<ResolveInfo> matches = pm.queryIntentActivities(emailIntent, 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)
            emailIntent.setClassName(best.activityInfo.packageName, best.activityInfo.name);
    
        activity.startActivity(emailIntent);
    }
    
    0 讨论(0)
  • 2020-12-01 06:56
    <TextView
     android:id="@+id/EmailId"
     android:linksClickable="true"
     android:autoLink="email"
     android:text="info@stackoverflow.com"
     />
    

    This is the best method to send email on click of textView.

    0 讨论(0)
  • 2020-12-01 06:57

    I don't know that you can specifically launch gmail. Have you tried this in your onClickListener

    Intent emailIntent = new Intent(Intent.ACTION_SEND);
    emailIntent.setType("text/plain");
    startActivity(emailIntent);  
    

    You can find more details here: Email android intent

    0 讨论(0)
  • 2020-12-01 06:57
    Intent intent = new Intent(Intent.ACTION_SEND).setType("text/plain")
                            .putExtra(Intent.EXTRA_EMAIL, new String[]{emails});
                    List<ResolveInfo> matches = activity.getPackageManager().queryIntentActivities(intent, 0);
                    ResolveInfo best = null;
                    for (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);
                    }
                    activity.startActivity(intent);
    
    0 讨论(0)
  • 2020-12-01 07:01

    You just place below code inside your click event. Will open directly gmail as compose mode, Output screenshot attached below.

    Happy coding :-)

    code :

    Intent intent=new Intent(Intent.ACTION_SEND);
    String[] recipients={"mailto@gmail.com"};
    intent.putExtra(Intent.EXTRA_EMAIL, recipients);
    intent.putExtra(Intent.EXTRA_SUBJECT,"Subject text here...");
    intent.putExtra(Intent.EXTRA_TEXT,"Body of the content here...");
    intent.putExtra(Intent.EXTRA_CC,"mailcc@gmail.com");
    intent.setType("text/html");
    intent.setPackage("com.google.android.gm");
    startActivity(Intent.createChooser(intent, "Send mail"));
    

    Output :

    0 讨论(0)
  • 2020-12-01 07:02

    As JeffC pointed out, it is easy to essentially tell Android that you want to send something email-like and have Android give users a list of choices, which will probably include GMail. If you specifically want GMail, you have to be a bit cleverer. (Note that the correct MIME type is actually "text/plain", not "plain/text". Do to an implementation oddity, GMail seems to be the only activity which responds to the latter, but this isn't a behavior I would count on.)

    The following App demonstrates the principle you can follow: actually examine all of the activities which say they can handle your SEND intent and see if any of them look like GMail.

    package com.stackoverflow.beekeeper;
    
    import android.app.Activity;
    import android.content.Intent;
    import android.content.pm.PackageManager;
    import android.content.pm.ResolveInfo;
    import android.os.Bundle;
    
    import java.util.List;
    
    public class StackOverflowTest extends Activity {
        /** Called when the activity is first created. */
        @Override
        public void onCreate(final Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            final Intent intent = new Intent(android.content.Intent.ACTION_SEND);
            intent.setType("text/plain");
            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);
            startActivity(intent);
        }
     }
    
    0 讨论(0)
提交回复
热议问题