Successful share intent for android

后端 未结 4 1752
长发绾君心
长发绾君心 2020-12-01 04:07

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

相关标签:
4条回答
  • 2020-12-01 04:45

    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

    0 讨论(0)
  • 2020-12-01 04:46

    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

    0 讨论(0)
  • 2020-12-01 05:03

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

    0 讨论(0)
  • 2020-12-01 05:04

    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.

    0 讨论(0)
提交回复
热议问题