13

I am using intent for send email with attachment,it is work fine,i want to get this email intent result,i already used startActivityforResult(),but i can't get result for email intent,how can we use startActivityforResult() for Email intent?

Thanks All

DeRagan
  • 22,827
  • 6
  • 41
  • 50
sivaraj
  • 1,849
  • 9
  • 35
  • 52
  • G-mail API does not return any intent result and its resultCode is 0 even if the email is not sent. If you need a different API, you have to implement your own, e.g. https://www.edumobile.org/android/send-email-on-button-click-without-email-chooser/ – Fenix May 28 '21 at 16:01

3 Answers3

15

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

fedj
  • 3,452
  • 1
  • 22
  • 21
  • i want to delete attachment image in sdcard after sending email,how to perform this task,please give some tips for me... – sivaraj Sep 23 '10 at 14:10
  • I don't think there's any tip to do that, Email activities don't give any clue on when the e-mail was sent. – fedj Sep 23 '10 at 14:21
  • @ok fedj, i want to send email with attach image,that image did not stored in sdcard,how can i attach and send email to user? – sivaraj Sep 23 '10 at 14:41
  • 4
    @sivaraj, don't ask questions N times, you already posted this kind of question, so read answers ... – fedj Sep 24 '10 at 13:21
  • If you had access to the logs or processes, maybe you could deduce it from there that an email was sent? – Christopher Masser Jun 08 '14 at 06:10
  • Now this is an awful design flaw in Androids API. Can't be for security or privacy reasons as the only result would just be *if* the user pressed the "send" action in the email activity. – philk Jun 22 '21 at 20:23
-1

There is no API like was suggested earlier. But... there is a workaround, though. The best way would be to use startActivityForResult(), instead of startActivity() to start the email intent. Then your onActivityResult method should look like this:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
   if (requestCode == YOUR_REQUEST_CODE){
    if (resultCode == RESULT_OK && data != null){
       //if data is not null, the user has clicked the send button
       //in his email application
     }

}

}

Remember that if you call startActivityForResult() from the fragment, always use your activity context, like context.startActivityForResult() and the onActivityResult method should be overridden in your host activity.

tung
  • 719
  • 2
  • 14
  • 30
-2

You sort of can, but it's ugly and inelegant. I'll work on smoothing this out. The main problem: After the email is sent you end up at a black screen with nothing but your app title at the top.

I'll do a 'hit enter to continue' or something if I have to.

Anyway: First snippet from main class writes the report to sdcard, then calls the activity that will send email.

WriteReportToStorage();

Intent Emailreport = new Intent(bvsactivity.this, Emailreport.class);
startActivityForResult(Emailreport,emailreport_ran);

Next, over in the emailreport class we do the standard email+attachment sending code:

public class Emailreport extends Activity {

@Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

final Intent email = new Intent(android.content.Intent.ACTION_SEND);
        email.setType("text/html

");
            email.putExtra(android.content.Intent.EXTRA_SUBJECT, "subject");
            email.putExtra(android.content.Intent.EXTRA_TEXT, "body");
            email.putExtra(android.content.Intent.EXTRA_STREAM, Uri.parse("file:/" +      Environment.getExternalStorageDirectory() + "//Report.html"));
            startActivity(Intent.createChooser(email, "Email:"));
        }

Lastly, back in your 'main' class, the onactivityresult that deletes the sdcard file:

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // Actions based on which menu item we chose.
        if (requestCode == emailreport_ran) {boolean deleted = reportfile.delete(); emailreport_ran = 1;}
        }
 }
Andro Selva
  • 53,910
  • 52
  • 193
  • 240
unrated
  • 25
  • 2
  • This gets a return from the activity, but with no confirmation on whether the email was sent or not. Not even on whether it was canceled straight away. The OP specifically said he/she already did that, ant wanted to get a valid resultCode instead. – zeh Feb 29 '12 at 16:28