1

I've been trying for a long time to get an alert, like the toast, to come up after the user has clicked on send button, and the e-mail has been sent. My code seem to be correct, but the toast don't show up. I've been searching a lot to get it right, but I'm stuck. Any help would be appreciated!

Regards Anders

//My code

public class mailer extends Activity {


private Button clickBtn;

@Override    
public void onCreate(Bundle savedInstanceState) {

  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);  
  clickBtn = (Button) findViewById(R.id.button1);
  //clickBtn.setText("Skicka info!");

  clickBtn.setOnClickListener(new OnClickListener() {

  @Override    
  public void onClick(View v) {

    Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);    
    String[] recipients = new String[]{"donald@oo.se","laura@oo.se"};    
    emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, recipients);
    emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Applikationsutveckling Android");    
    emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, "Jag vill veta mer om utbildningen!");    
    emailIntent.setType("text/plain");
    startActivityForResult(Intent.createChooser(emailIntent, "Skicka e-post..."),1);    

}
});
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == RESULT_OK) {
        if (requestCode == 1) {
            Toast.makeText(mailer.this, 
                    "Tack för din intresseanmälan!", 
                    Toast.LENGTH_SHORT).show();
        }
    }
}


}
Vladimir Ivanov
  • 42,730
  • 18
  • 77
  • 103
anders
  • 11
  • 5

2 Answers2

3

Sorry, you might need to find something else, but I don't believe you'll be able to make this work. As far as I can tell the default Android email programs (Gmail, Email) typically return prior to the actual transmission of the email, and in any case generally do not communicate using the onActivityResult pattern.

Femi
  • 64,273
  • 8
  • 118
  • 148
3

try this ..

if(requestCode==1 && resultCode==Activity.RESULT_OK)    
        {
            Toast.makeText(Activity, "Mail sent.", 
                              Toast.LENGTH_SHORT).show();

        }
        else if (requestCode==1 && resultCode==Activity.RESULT_CANCELED)
        {
            Toast.makeText(Activity, "Mail canceled.", 
                                Toast.LENGTH_SHORT).show();

        }
        else 
        {
            Toast.makeText(Activity, "Plz try again.", 
                               Toast.LENGTH_SHORT).show();

        }
Niranj Patel
  • 32,980
  • 10
  • 97
  • 133
  • Thank you for your suggestion. I´ve tweaked the code to avoid errors, as good as can. The mail goes away and the toast "Mail canceled." goes away. I don´t know why..Of corse i could program "bad way" and change mail canceled to thank you! But maybe there´s a better way? – anders Apr 28 '11 at 16:12
  • public void onActivityResult(int requestCode, int resultCode, Intent data) { if(requestCode==1 && resultCode==RESULT_OK) { Toast.makeText(getApplicationContext(), "Tack för visat intresse!", Toast.LENGTH_SHORT).show(); //This options show of and the mail goes away correct... else if (requestCode==1 && resultCode==Activity.RESULT_CANCELED) { Toast.makeText(getApplicationContext(), "E-post skickad.", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(getApplicationContext(), "Försök igen...", Toast.LENGTH_SHORT).show(); – anders Apr 28 '11 at 16:19
  • Hi! Someone having a suggestion? – anders Apr 29 '11 at 17:55