Show Toast after email send in android?

回眸只為那壹抹淺笑 提交于 2019-12-23 11:22:44

问题


I am using the below code to send the mail

Intent i = new Intent(Intent.ACTION_SEND);
i.setType("text/plain");
i.putExtra(Intent.EXTRA_EMAIL, new String[] { "abc@yahoo.com" });
i.putExtra(Intent.EXTRA_CC, new String[] { bcc_string });
i.putExtra(Intent.EXTRA_SUBJECT, "Video Suggest");
i.putExtra(Intent.EXTRA_TEXT, url_link); 

try {
   startActivityForResult(Intent.createChooser(i, "Send Mail..."), 1);
} catch (android.content.ActivityNotFoundException ex) {
   Toast.makeText(AllVideos.this, "There are no email clients installed.", Toast.LENGTH_SHORT)
   .show();
}

and in my on activity result i am using the following code

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    // System.out.println("inactivity");
    // Toast.makeText(AllVideos.this, "Mail Send", 10).show();

    System.out.println("inside activity result");

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

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

But everytime i send or discard the message i get "mail cancelled" toast. Please help me put with this.

Thanks in advance.


回答1:


as per link

You can't, this is not part of the API. It returns once you have pressed send button even if it is not sent

ACTION_SEND does NOT have any output as a result you always get the default value which is RESULT_CANCELED.

Also you can NOT check it with Intent data coming back because it is always null either mail send or discard.




回答2:


Try this way.

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

        } 
        else if (resultCode == RESULT_CANCELED) 
        {
            Toast.makeText(this, "Mail canceled.", Toast.LENGTH_SHORT)
                    .show();
        } 
        else 
        {
            Toast.makeText(this, "Plz try again.", Toast.LENGTH_SHORT)
                    .show();
        }
  }


来源:https://stackoverflow.com/questions/11154772/show-toast-after-email-send-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!