Is it possible to detect if an ACTION_SEND Intent suceeded?

和自甴很熟 提交于 2020-12-26 06:58:32

问题


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?


回答1:


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.




回答2:


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();

    }
}


来源:https://stackoverflow.com/questions/45850756/is-it-possible-to-detect-if-an-action-send-intent-suceeded

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