Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to share different texts based on package name in Android 10?

My app shares a specific URL to open in other apps, but I want to use a custom URL depending on what app the user is sharing it with. For example, with Gmail I want to use myurl.com?src=gmail, and with FB I want to use myurl.com?src=fb etc.

This normally would have worked with this famous method: https://stackoverflow.com/a/18068122/3015986. However, in Android 10, that solution no longer works anymore: https://medium.com/@AndroidDeveloperLB/this-wont-work-anymore-on-android-q-1702c19eb7bb

So, what other options are there?

like image 442
BeLambda Avatar asked Nov 06 '22 08:11

BeLambda


1 Answers

Well there are always options. In this particular case(with the given conditions regarding URL query parameter) I would suggest to create separate sharing method for each platform you want to share your content with rather than use the Android default sharing method.

For example here is a custom sharing for Facebook, etc. The method is quite time consuming though.

But there is a better option to handle such cases. It is a relatively new method to get info about which app was chosen. It is easy. Basically you will need to:

  • Create a PendingIntent for a BroadcastReceiver and supply its IntentSender in Intent.createChooser() like this:
Intent share = new Intent(ACTION_SEND);
...
PendingIntent pi = PendingIntent.getBroadcast(myContext, requestCode,
                new Intent(myContext, MyBroadcastReceiver.class),
                FLAG_UPDATE_CURRENT);
share = Intent.createChooser(share, null, pi.getIntentSender());
  • Receive the callback in MyBroadcastReceiver and look in Intent.EXTRA_CHOSEN_COMPONENT like this:
@Override public void onReceive(Context context, Intent intent) {
  ...
  ComponentName clickedComponent = intent.getParcelableExtra(EXTRA_CHOSEN_COMPONENT);
}

More info here

Though you cannot change your URL for each specific case because the sharing event has already occurred, you can implement separate API call or URL link using which you can gather analytics data, change the outcome of the URL click you sent(if you control the backend, of course) or just show a customized message to the user of your app.

Keep in mind, that for some usecases of this method, URL you share should be unique per user(for ex. to change the outcome of the URL click etc).

Hope it helps.

like image 169
Pavlo Ostasha Avatar answered Nov 14 '22 22:11

Pavlo Ostasha