电子邮件意图消息后祝酒

时间:2012-03-22 14:55:05

标签: android-intent toast email-client

我的应用程序中设置了电子邮件意图,并且在发送电子邮件后尝试向用户致敬。

这是“系统流程”

用户点击按钮 - >电子邮件选择器显示 - >在客户端发送电子邮件 - > “发送电子邮件”通用吐司显示 - >自定义吐司

我尝试在电子邮件意图和onPause方法之后添加toast语法。

任何人都可以帮助我吗?这是我的示例代码。

代码:

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

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

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.service);
        initializeVars();
        sendEmail.setOnClickListener(this);
    }

    // Use the following commented fields for future Spinner properties
    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);
        vehicleYear = (EditText) findViewById(R.id.tYear);
        vehicleMake = (EditText) findViewById(R.id.tMake);
        vehicleModel = (EditText) findViewById(R.id.Model);
        serviceDate = (TextView) findViewById(R.id.tpickDate);
        // serviceTime = (Spinner) findViewById(R.id.sTime);
        // serviceNeeded = (Spinner) findViewById(R.id.sNeeded);
        serviceTime = (EditText) findViewById(R.id.tTime);
        serviceNeeded = (EditText) findViewById(R.id.tNeeded);
        comments = (EditText) findViewById(R.id.eComments);
        sendEmail = (Button) findViewById(R.id.bSentEmail);
        // PickDate = (Button) findViewById(R.id.pickDate);
    }

    public void onClick(View v) {

        // TODO Auto-generated method stub
        conversion();
        String emailaddress[] = { "email@example.com" };
        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("vnd.android.cursor.dir/email");
        emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[] { "email@example.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"));
        Toast.makeText(ScheduleService.this, R.string.myString, Toast.LENGTH_SHORT).show();
    }

    private void conversion() {
        // 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();// Text field substitute
                                                        // for spinner adapter
        vehicleManu = vehicleMake.getText().toString(); // Text field substitute
                                                        // for spinner adapter
        // vehicleYears = (String) vehicleYear.getAdapter().getItem(RESULT_OK);
        // vehicleManu = (String) vehicleMake.getAdapter().getItem(RESULT_OK);
        vehicleModeltrim = vehicleModel.getText().toString();
        serviceAppointment = serviceDate.getText().toString();
        serviceAppointmentTime = serviceTime.getText().toString();
        serviceTypeNeeded = serviceNeeded.getText().toString();

        // serviceAppointmentTime = (String) serviceTime.getAdapter().getItem(
        // RESULT_OK);
        // /serviceTypeNeeded = (String) serviceNeeded.getAdapter().getItem(
        // RESULT_OK);
        extraComments = comments.getText().toString();
    }

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        finish();

    }
}

1 个答案:

答案 0 :(得分:3)

更新:我想我误解了你的问题;从我现在可以告诉你,你想要在发送电子邮件和电子邮件应用程序的吐司后显示你的吐司吗?如果是这样的话,试试这个:

首先从finish()方法中移除onPause()

然后,将startActivity替换为startActivityForResult,其中需要额外参数requestCoderequestCode用于将结果与不同的活动分开。

startActivityForResult(Intent.createChooser(emailIntent, "Please select Email Client"), 
    MY_REQUEST_CODE); // MY_REQUEST_CODE is some constant int >= 0

最后,将以下代码添加到接收startActivityForResult 的结果。在这里,您可以根据结果决定要显示哪个吐司。

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    Log.i("SS", "onActivityResult: " + requestCode + ", " + resultCode + ", " 
        + (data != null ? data.toString() : "empty intent"));
    if(requestCode == MY_REQUEST_CODE) {
        if(resultCode == RESULT_OK) {
            Toast.makeText(getApplicationContext(), R.string.myString,
                Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(getApplicationContext(), R.string.myOtherString, 
                Toast.LENGTH_SHORT).show();
        }
    }
    finish(); // to end your activity when toast is shown
}

如果这是你真正需要的,我很抱歉我一开始并没有清楚地看到你的问题。


旧思想:

以下是一个小应用程序,它只会快速切换您的手动操作(按钮)以发送电子邮件意图。这对我来说很好。如果这对你也有用,那么你可能从按下的按钮调用“发送意图”方法有一些怪癖。

package my.test;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;

public class AndroidTestActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Toast.makeText(AndroidTestActivity.this, "Welcome toast", Toast.LENGTH_SHORT).show();
        Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
        emailIntent.setType("vnd.android.cursor.dir/email");
        emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{"bfdk@dgfsgfds.dfs"});
        emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Schedule Service Request");
        emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, "Message goes here");
        startActivity(Intent.createChooser(emailIntent, "Please select Email Client"));
        Toast.makeText(AndroidTestActivity.this, R.string.myString, Toast.LENGTH_SHORT).show();
    }

    @Override
    protected void onPause() {
        super.onPause();
        finish();
        Toast.makeText(getApplicationContext(), "Pause toast", Toast.LENGTH_SHORT).show();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Toast.makeText(this, "Destroy toast", Toast.LENGTH_SHORT).show();
    }
}