Successful share intent for android

后端 未结 4 1787
长发绾君心
长发绾君心 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: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:

    
    

    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

提交回复
热议问题