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

后端 未结 11 997
别跟我提以往
别跟我提以往 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 07:02
    Intent intent = new Intent(Intent.ACTION_SEND);
            String[] recipients = {"recipient@gmail.com"};//Add multiple recipients here
            intent.putExtra(Intent.EXTRA_EMAIL, recipients);
            intent.putExtra(Intent.EXTRA_SUBJECT, "Mail Subject"); //Add Mail Subject
            intent.putExtra(Intent.EXTRA_TEXT, "Enter your mail body here...");//Add mail body
            intent.putExtra(Intent.EXTRA_CC, "mailcc@gmail.com");//Add CC emailid's if any
            intent.putExtra(Intent.EXTRA_BCC, "mailbcc@gmail.com");//Add BCC email id if any
            intent.setType("text/html");
            intent.setPackage("com.google.android.gm");//Added Gmail Package to forcefully open Gmail App
            startActivity(Intent.createChooser(intent, "Send mail"));
    
    0 讨论(0)
  • 2020-12-01 07:11

    if you don't get anything in this line

    final List<ResolveInfo> matches = pm.queryIntentActivities(intent, 0);

    then replace this line with

    final List<ResolveInfo> matches = pm.queryIntentActivities(intent, 1);

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

    This code will directly start the gmail application to send an email.

    I found out using this post that the important part here is to find the "packageName" and the "activityInfo.name"

    I wanted to only use gmail without a chooser. Note that the package name is hard coded so if Gmail changes its packagename it won't work any more.

    The key to this was the setComponent where the first param is the package name and the second param is the activityInfo name.

    But like i said use with caution, I repeat myself; if the user doesn't have the gmail app installed or gmail changes its package name or Activty name to send an email this (hard)code will break. Thy have been warned ;)

    Here is my code

    Intent myIntent = new Intent(Intent.ACTION_SEND);
    
    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[]{"exampleto@gmail.com"});
            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);
    
    0 讨论(0)
  • 2020-12-01 07:20
    try{    
        Intent intent = new Intent (Intent.ACTION_VIEW , Uri.parse("mailto:" + "your_email"));
        intent.putExtra(Intent.EXTRA_SUBJECT, "your_subject");
        intent.putExtra(Intent.EXTRA_TEXT, "your_text");
        startActivity(intent);
    }catch(ActivityNotFoundException e){
        //TODO smth
    }
    
    0 讨论(0)
  • 2020-12-01 07:21

    You can use Simple Intent.ACTION_SEND intent set Intent.EXTRA_EMAIL for array of emails set Intent.EXTRA_SUBJECT for subject line in email composer Explore more EXTRA options available here -> https://developer.android.com/guide/components/intents-common#Email

    Here's a quick code snippet

    Intent intent = new Intent(Intent.ACTION_SEND);
                    intent.setType("*/*");
                    intent.putExtra(Intent.EXTRA_EMAIL, new String[]{"test@gmail.com"});
                    intent.putExtra(Intent.EXTRA_SUBJECT, "Feedback");
                    if (intent.resolveActivity(ctx.getPackageManager()) != null) {
                        startActivity(intent);
                    }
    
    0 讨论(0)
提交回复
热议问题