sivaraj
sivaraj

Reputation: 1867

how can we use startActivityforResult() for Email intent?

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

Upvotes: 13

Views: 13824

Answers (3)

Nazar Dovhyy
Nazar Dovhyy

Reputation: 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.

Upvotes: -1

unrated
unrated

Reputation: 25

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

Upvotes: -2

fedj
fedj

Reputation: 3452

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

Upvotes: 15

Related Questions