Email Intent Email Chooser

2019-07-18 19:54发布

问题:

I'm having an issue with my application. The purpose of the app is to collect data and submit the data form using javamail to a specified email address. when the user clicks the button, they should receive a prompt to select an email client, however I am not getting this prompt when I test it. Can someone tell me what I am missing?

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

public class SubmitForm extends Activity implements View.OnClickListener {
private Intent emailIntent;
EditText personsFirstName, personsLastName, personsEmail, personsPhone, comments, vehicleModel;
Spinner vehicleYear, vehicleMake, serviceTime, serviceNeeded;
TextView serviceDate;
String fname, lname, emailAdd, phoneNumber, vehicleYears, vehicleModeltrim, vehicleManu, serviceAppointment, serviceAppointmentTime, serviceTypeNeeded, extraComments;
Button sendEmail;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.service);
    initializeVars();
    sendEmail.setOnClickListener(this);
}
private void initializeVars() {
    // TODO Auto-generated method stub
    personsFirstName = (EditText) findViewById(R.id.firstName);
    personsLastName = (EditText) findViewById(R.id.lastName);
    personsEmail = (EditText) findViewById(R.id.Email);
    personsPhone = (EditText) findViewById(R.id.PhoneNumber);
    vehicleYear = (Spinner) findViewById(R.id.sYear);
    vehicleMake = (Spinner) findViewById(R.id.sMake);
    vehicleModel = (EditText) findViewById(R.id.Model);
    serviceDate = (TextView) findViewById(R.id.Model);
    serviceTime = (Spinner) findViewById(R.id.sTime);
    serviceNeeded = (Spinner) findViewById(R.id.sNeeded);
    comments = (EditText) findViewById(R.id.eComments);
    sendEmail = (Button) findViewById(R.id.bSubmit);
}
public void onClick(View v) {
    // TODO Auto-generated method stub
    convertToString();
    String emailaddress[] = { emailAdd };
    String message = "Please Review the following"
            + '\n' + " "
            + '\n' + "First Name: " + fname
            + '\n' + "Last Name: " + lname
            + '\n' + "Email :" + emailAdd
            + '\n' + "Phone Number: " + phoneNumber
            + '\n' + "Vechile Year: " + vehicleYears
            + '\n' + "Vehicle Make: " + vehicleManu
            + '\n' + "Vehicle Model: " + vehicleModeltrim
            + '\n' + "Requested Service Date: " + serviceAppointment
            + '\n' + "Requested Service Time: " + serviceAppointmentTime
            + '\n' + "Service Needed: " + serviceTypeNeeded
            + '\n' + "Comments: " + extraComments;

    Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
    emailIntent.setType("plain/text");
    emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[] {"test@someemail.com"});

    emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, emailaddress);
    emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Schedule Service Request");

    emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, message);
    startActivity(Intent.createChooser(emailIntent, "Please select Email Client"));
}
private void convertToString() {
    // TODO Auto-generated method stub

    fname = personsFirstName.getText().toString();
    lname = personsLastName.getText().toString();
    emailAdd = personsEmail.getText().toString();
    phoneNumber = personsPhone.getText().toString();

    //vehicleYears = vehicleYear.getText().toString();
    vehicleYears = (String) vehicleYear.getAdapter().getItem(RESULT_OK);


    //vehicleManu = vehicleMake.getText().toString();
    vehicleManu = (String) vehicleMake.getAdapter().getItem(RESULT_OK);

    vehicleModeltrim = vehicleModel.getText().toString();
    serviceAppointment = serviceDate.getText().toString();
    serviceAppointmentTime = (String) serviceTime.getAdapter().getItem(RESULT_OK);


    //serviceTypeNeeded = serviceNeeded.getText().toString();
    serviceTypeNeeded = (String) serviceNeeded.getAdapter().getItem(RESULT_OK);

    extraComments = comments.getText().toString();
}
@Override
protected void onPause() {
    // TODO Auto-generated method stub
    super.onPause();
    finish();
}
}

回答1:

if u want only to choose among email client install on device you can make use of ACTION_SENTTO option

String mailTo="email@gmail.com";
Intent email_intent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts("mailto",mailTo, null)); 
email_intent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Subject text here"); 
email_intent.putExtra(android.content.Intent.EXTRA_TEXT,"Body text here"); 

startActivity(Intent.createChooser(email_intent, "Send email..."));

try this email intent on real device...emulator might not give you the result..

good luck..