Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to detect if an ACTION_SEND Intent suceeded?

I have a simple Android app with code like this (from the Android Documentation):

Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send.");
sendIntent.setType("text/plain");
startActivity(Intent.createChooser(sendIntent, getResources().getText(R.string.send_to)));

But I can't find a way to detect of the email was successfully sent (or the user cancels out). Is there a way to read an intent response?

like image 642
Jason Sperske Avatar asked Aug 23 '17 23:08

Jason Sperske


2 Answers

Is there a way to read an intent response?

Not from an arbitrary activity for an arbitrary action.

The documentation for Intent actions will tell you if there is expected output or not. So, for example, ACTION_GET_CONTENT is documented to have output. For those Intent actions, you use startActivityForResult(), and part of the output will be a "result code" to let you know generally what the result was.

However:

  • Not every Intent action is documented to have output. Notably, ACTION_SEND is not documented to have output. In that case, you don't use startActivityForResult() (but instead use startActivity()). Even if you do use startActivityForResult(), you have no way to know if a negative outcome means that the user cancelled out or if the other activity simply is following the documentation and did not return a result.

  • Some activities are buggy and fail to return results when they should.

  • Your definition of a successful result and the activity's definition of a successful result may differ.

like image 96
CommonsWare Avatar answered Nov 05 '22 05:11

CommonsWare


I don't think there is an assured way to do it.

You could initiate the send using startActivityForResult() and hope that the activity which handles the Intent replies with a RESULT_OK. But you can't rely on it to work always.

AnD then you can check

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == RESULT_OK) {
        Toast.makeText(this,"Successfully Sharing", Toast.LENGTH_SHORT).show();

    }
}
like image 28
mmucito Avatar answered Nov 05 '22 04:11

mmucito