Search code examples
android-intenttoastemail-client

Toast after email intent message


I have an email intent set in my application, and im trying to toast the user after the email is sent.

Here is the "system flow"

User clicks button -> email chooser displays -> send email in client -> "Sending email" generic toast displays -> Custom toast

I have tried putting the toast syntax after the email intent, and onPause method.

Can anyone assist me? here is my sample code.

Code:

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

    }
}

Solution

  • UPDATE: I think I have misunderstood your problem; from what I can tell now, you want YOUR toast to appear AFTER the email is sent and the email application's toast has been displayed? If that is the case try this:

    First remove the finish() from your onPause() method.

    Then, replace your startActivity with startActivityForResult which takes an extra argument requestCode. The requestCode is used to separate the results from different activities.

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

    Finally add the following code to receive the result from your startActivityForResult. Here you can decide which toast to display, depending on the result.

    @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
    }
    

    If this is what you actually need, I'm sorry I didn't see your problem clearly at first.


    Old thoughts:

    The following is a small application that only shortcuts your manual action (the button) for sending the email intent. This works fine for me. If this works for you too, then maybe your way of calling the "send intent" method from the pressed button has some quirks.

    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[]{"[email protected]"});
            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();
        }
    }