电子邮件意图电子邮件选择器

时间:2012-03-13 18:16:45

标签: java android android-intent onclicklistener

我的申请存在问题。该应用程序的目的是收集数据并使用javamail将数据表单提交到指定的电子邮件地址。当用户单击该按钮时,他们应该收到选择电子邮件客户端的提示,但是在测试时我没有收到此提示。有人能告诉我我错过了什么吗?

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();
}
}

2 个答案:

答案 0 :(得分:10)

如果您只想在设备上选择电子邮件客户端,则可以使用ACTION_SENTTO选项

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..."));

在真实设备上尝试此电子邮件意图...模拟器可能无法提供结果..

祝你好运..

答案 1 :(得分:0)

仅作记录:

某些电子邮件应用程序(例如Gmail)期望EXTRA_EMAIL和EXTRA_CC等附加功能使用String [],请使用putExtra(String,String [])将它们添加到您的意图中。

Android developer page

val sendIntent: Intent = Intent().apply {
            action = Intent.ACTION_SEND
            putExtra(Intent.EXTRA_EMAIL, arrayOf("a@bc.de"))
            putExtra(Intent.EXTRA_SUBJECT, "subject")
            putExtra(Intent.EXTRA_TEXT, "content")
            type = "text/plain"
}

startActivity(Intent.createChooser(sendIntent, "Send mail"))