Branching the Android Share Intent extras depending on which method they choose to share
Asked Answered
C

1

14

Simply want to share a dynamic text string + the URL to the app. The native Android share intent is setup correctly, and works perfect with Twitter, Gmail, etc. But, as many will guess, it does not work with Facebook. Appearantly because Facebook will not accept text in the intent.EXTRA_TEXT field, only a single URL.

Well, my question to y'all is: is there a way to branch off the share intent extras depending on which method they choose to share? for example, if they share via gmail or Twitter, use the existing String + URL (the desired option) EXTRA_TEXT, but if they choose to share via Facebook, only use a URL as the EXTRA_TEXT.

Not really wanting to implement the Facebook Android SDK for such a simple task that is built-in natively in Android.

Appreciate your time and advice.

Tried something like this, but it obviously fails because its only checking if the sharing option exists (when share pops up, not after they click a share method), it doesn't respond when they choose a method.

  String shareBody = "app string text " + act_txt + " more text! Get the app at http://www.appurl.com";

 PackageManager pm = view.getContext().getPackageManager();
    List<ResolveInfo> activityList = pm.queryIntentActivities(sharingIntent, 0);
    for(final ResolveInfo app : activityList) {
         Log.i(TAG, "app.actinfo.name: " + app.activityInfo.name);
        //if((app.activityInfo.name).contains("facebook")) {
              if("com.facebook.katana.ShareLinkActivity".equals(app.activityInfo.name)) {


            sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, "http://www.appurl.com");
            startActivity(Intent.createChooser(sharingIntent, "Share idea"));
            break;
        } else {
            sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "app name");
            sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
            startActivity(Intent.createChooser(sharingIntent, "Share"));
            break;
        }
    }
Circumambient answered 22/1, 2013 at 2:59 Comment(0)
C
20

found a solution, on this SO question asking for something else: https://mcmap.net/q/52866/-custom-filtering-of-intent-chooser-based-on-installed-android-package-name

the attempt posted my original question here was close. within that cycle of possible shareIntent List, you need to create a new share intent targeted at the specific sharing choice (like facebook or twitter)

here is a final working solution that shares only a URL if facebook is choosen, otherwise shares the complete text string + url:

public void shareIt(View view){
    //sharing implementation
    List<Intent> targetedShareIntents = new ArrayList<Intent>();
    Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
    sharingIntent.setType("text/plain");
    String shareBody = "string of text " + txt_var + " more text! Get the app at http://someapp.com";

    PackageManager pm = view.getContext().getPackageManager();
    List<ResolveInfo> activityList = pm.queryIntentActivities(sharingIntent, 0);
    for(final ResolveInfo app : activityList) {

         String packageName = app.activityInfo.packageName;
         Intent targetedShareIntent = new Intent(android.content.Intent.ACTION_SEND);
         targetedShareIntent.setType("text/plain");
         targetedShareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "share");
         if(TextUtils.equals(packageName, "com.facebook.katana")){
             targetedShareIntent.putExtra(android.content.Intent.EXTRA_TEXT, "http://someurl.com");
         } else {
             targetedShareIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
         }

         targetedShareIntent.setPackage(packageName);
         targetedShareIntents.add(targetedShareIntent);

    }

    Intent chooserIntent = Intent.createChooser(targetedShareIntents.remove(0), "Share Idea");

    chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, targetedShareIntents.toArray(new Parcelable[]{}));
    startActivity(chooserIntent);

}
Circumambient answered 25/1, 2013 at 0:56 Comment(3)
why you remove(0) from targetedShareIntentsBefoul
The remove(0) is called because the intent that is passed to createChooser will be "wrapped" again. So the result would be 2 times the intent on position 0. But I found a different solution. Just define an Intent with a package name that does not exist. - Because the remove(0) lead to an chooser where the Intent on position 0 was separated in a new line beneath all other intents. I didn't want that.Behlau
EDIT: Seems like on lower APIs (Tested with API 19) it is not possible to use an Intent with a package name that does not exist.Behlau

© 2022 - 2024 — McMap. All rights reserved.