0

I am developing an app that allows the user to start an activity that shares some sound file. I want to know if the user canceled sharing of sounds.

My Code:

                Intent sendIntent = new Intent();
                sendIntent.setAction(Intent.ACTION_SEND);
                sendIntent.putExtra(Intent.EXTRA_STREAM,Uri.parse(Utils.createFile(Utils.getRingtone_name()[1], "share")));
                sendIntent.setType("audio/*");
                startActivity(sendIntent);

I want to do some action if the user cancelled sharing. How can I achieve this?

NoDataDumpNoContribution
  • 10,591
  • 9
  • 64
  • 104
karimkhan
  • 321
  • 3
  • 18

2 Answers2

2

I am developing an app that in it user share some sound file

You have not described how you are doing this. For this answer, I am going to guess that you are referring to ACTION_SEND or ACTION_SENDTO, as those are the typical Intent actions used for "share" sorts of operations.

If this guess is incorrect, please edit your question to provide the actual Java code that you are using to "share some sound file".

i want to know when user cancel sharing of sounds

There is no way for you to know what the user did, or did not do, with the content that you share via ACTION_SEND or ACTION_SENDTO.

If you are implementing your own ContentProvider for serving the sound file, you can know whether another app has requested that content. However, that still does not imply that the user actually completed some action with that content. For example, if the other app is an email client, it might have read the content to create an email attachment for its compose window, but then the user elected not to send the email.

User can cancel this by pressing back button

Pressing the BACK button does not necessarily imply that the user did not share the content.


Based on your comments, I think you are asking a different question: how do you know if the user chose something from the activity chooser that may appear when you try to share something?

On Android 5.1 and higher, you can use the three-parameter createChooser() method to arrange to find out what the user chose through the standard system chooser dialog. Prior to that, the only way that I know of to know what the user chose is to not use the system chooser dialog, but to handle that yourself. You can create your own "chooser", to choose among the available options given to you by queryIntentActivities() on PackageManager.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • About this section of your answer "Pressing the BACK button does not necessarily imply that the user did not share the content." i must say that pressing back button close share Intent and this means that user canceled sharing. am i wrong with this? – karimkhan Dec 08 '15 at 19:03
  • 1
    @karimkhan: You have no idea what the UI of the activity that you started up looks like. Maybe it consists of a big "share it!" button that when clicks uploads the content somewhere, but then the user has to press BACK to return to your app. There is no requirement that the other app automatically finish the activity that you started when some "share" operation is done (success or failure). – CommonsWare Dec 08 '15 at 19:05
  • may be i can not say what i want. but i just want to know when share dialog(that user choose apps for sharing) is finished. can i know this? – karimkhan Dec 08 '15 at 19:09
  • 1
    @karimkhan: I have updated my answer to see if I am now covering what you are seeking. – CommonsWare Dec 08 '15 at 19:14
  • Thanks but still it is not what i want. I want to know when app chooser dialog is finished. as you know this dialog be closed when user press BACK button. – karimkhan Dec 08 '15 at 19:21
  • Sorry to interrupt. Thought I can add my words if it helps karimkhan. @Karimkhan, your expectation is correct if you have started it with startActivityResult() but you just post an intent for which you don't get any result back. – cgr Dec 08 '15 at 19:25
  • 2
    @karimkhan: "I want to know when app chooser dialog is finished" -- that is what I covered in my update. "as you know this dialog be closed when user press BACK button" -- it will be closed for many reasons, including the BACK button. You have no way of knowing *anything* about that dialog's state, except on Android 5.1+, by using the three-parameter `createChooser()`. Since you find that to be unacceptable, you will need to write your own replacement for the app chooser dialog in your own app, so you can monitor what the user does. – CommonsWare Dec 08 '15 at 19:26
  • @cgr: yes i want to know share dialog results. as you know user can cancel sharing by pressing BACK button and in this state share dialog(app chooser dialog) will gone. I want to know this time. – karimkhan Dec 08 '15 at 19:29
  • Thanks @CommonsWare and cgr. that is clear for me now. – karimkhan Dec 08 '15 at 19:42
  • I got what you are expecting. You can not know that unfortunately. See the other posts I have provided in my answer. Search through SO to find more posts on this. I recently came across that requirement but got to know that we can not know the result. That is how it works. – cgr Dec 08 '15 at 19:44
  • @cgr: thanks my friend. – karimkhan Dec 08 '15 at 20:57
  • You're welcome. But you can still pass custom text along with intent, in case if you required. You may vote up. – cgr Dec 08 '15 at 21:13
1

There is no way for you to know whether the sharing intent was successful or not OR user really shared it or discarded. Does not matter what you share - text, image, sound or video etc.

I think you are asking exactly this - Successful share intent for android.

Community
  • 1
  • 1
cgr
  • 4,578
  • 2
  • 28
  • 52