43

How can i tell if a user successfully completed a share intent? For instance if a user wanted to share an app via Facebook or Twitter.

Edit:

I am not looking at how to create an Intent for sharing. I want to know if the user actually shared anything. Or did the user hit the cancel button.

Alex Lockwood
  • 83,063
  • 39
  • 206
  • 250
Steve D
  • 435
  • 1
  • 4
  • 6
  • 1
    Why does your app need to know this? – adamp Jan 09 '12 at 19:47
  • 1
    I was looking to thank the user for sharing my app. But I don't want to thank them if they didn't share. – Steve D Jan 10 '12 at 02:19
  • 1
    @adamp this information might be important for statistical reasons. it's nice to know where your content is shared most to optimize for that target. – dy_ Nov 18 '15 at 20:50

4 Answers4

25

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.

HRJ
  • 17,079
  • 11
  • 56
  • 80
  • I guess i could just log.v the reply for each app that might be used and use a switch("reply for each app") from there. – Steve D Jan 13 '12 at 20:57
  • hi, i have the same issue. can you please share your result if you succeed please. Thanks :) – yakusha Jul 20 '13 at 03:47
  • 12
    I find that most sharing services respond with 0, which is no different than if the share is cancelled (also 0). Twitter returns -1 but that is it. Looks like we can't track shares :( – Kevin_TA May 01 '14 at 20:08
  • I tested WhatsApp, Signal, Twitter and Gmail and only WhatsApp returned RESULT_OK. All others returned RESULT_CANCELED. – G00fY Feb 04 '21 at 14:17
2

For twitter - the "data" object in OnActivityResult is null when the user cancels the share.

Rony Tesler
  • 1,207
  • 15
  • 25
2

Found the option, suitable for Android >= 22. Maybe it can help somebody.

Starting with Android 22 there's an option to send IntentSender object in createChooser method. You can create a pending intent for a broadcast receiver in which you can get the package name of the app on which a user clicked.

Receiver:

public class MyReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
    super.onReceive(context, intent);
    // do something here
}
}

Manifest:

<receiver android:name="MyReceiver" android:exported="false"/>

Creating pending intent:

PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, receiver, PendingIntent.FLAG_UPDATE_CURRENT);

And then using it in the chooser intent:

startActivity(Intent.createChooser(share
                            , "some_title"
                            , pendingIntent.getIntentSender()));

Then in onReceiver you can get the package name of the app:

String selectedAppPackage = String.valueOf(intent.getExtras().get(EXTRA_CHOSEN_COMPONENT))

Source: medium blogpost

A. Kazarovets
  • 1,607
  • 1
  • 12
  • 10
-10

You have use the Intent.ACTION_SEND, and the system will display a list of applications (on the device) where you can share. This website explains how:

http://sudarmuthu.com/blog/sharing-content-in-android-using-action_send-intent

Alex Lockwood
  • 83,063
  • 39
  • 206
  • 250
xagema
  • 775
  • 1
  • 7
  • 14
  • 9
    I am not looking at how to create an intent for sharing. I want to know if the user actually shared anything. Or did the user hit the cancel button. – Steve D Jan 09 '12 at 19:33