Android share intent for facebook- share text AND link

Solution 1:

I just built this code and it's working for me:

private void shareAppLinkViaFacebook(String urlToShare) {
    try {
        Intent intent1 = new Intent();
        intent1.setClassName("com.facebook.katana", "com.facebook.katana.activity.composer.ImplicitShareIntentHandler");
        intent1.setAction("android.intent.action.SEND");
        intent1.setType("text/plain");
        intent1.putExtra("android.intent.extra.TEXT", urlToShare);
        startActivity(intent1);
    } catch (Exception e) {
        // If we failed (not native FB app installed), try share through SEND
        String sharerUrl = "https://www.facebook.com/sharer/sharer.php?u=" + urlToShare;
        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(sharerUrl));
        startActivity(intent);
    }
}

Solution 2:

If you are going to use the regular Android Sharing Intents, then unfortunately the Facebook sharing intent can only take a single URL (make sure it has http://) and no additional text message. It is a strange limitation that doesn't really make sense.

You have to use the actual official separate Facebook Android SDK in your project to get the full sharing functionality. Which is extra work.

I ran in to similar issues. In the end, what I did was branch the intent. If they choose to share (in the regular android share intent) via Facebook, create a new share intent that only has the URL and push that to facebook. All other share options (twitter, message, email) would work like normal.

my question and solution are here:

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

  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;
    }
}

Solution 3:

In Lollipop (21), you can use Intent.EXTRA_REPLACEMENT_EXTRAS to override the intent for specific apps.

https://developer.android.com/reference/android/content/Intent.html#EXTRA_REPLACEMENT_EXTRAS

private void doShareLink(String text, String link) {
  Intent shareIntent = new Intent(Intent.ACTION_SEND);
  shareIntent.setType("text/plain");
  Intent chooserIntent = Intent.createChooser(shareIntent, getString(R.string.share_via));

  // for 21+, we can use EXTRA_REPLACEMENT_EXTRAS to support the specific case of Facebook
  // (only supports a link)
  // >=21: facebook=link, other=text+link
  // <=20: all=link
  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    shareIntent.putExtra(Intent.EXTRA_TEXT, text + " " + link);
    Bundle facebookBundle = new Bundle();
    facebookBundle.putString(Intent.EXTRA_TEXT, link);
    Bundle replacement = new Bundle();
    replacement.putBundle("com.facebook.katana", facebookBundle);
    chooserIntent.putExtra(Intent.EXTRA_REPLACEMENT_EXTRAS, replacement);
  } else {
    shareIntent.putExtra(Intent.EXTRA_TEXT, link);
  }

  chooserIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  startActivity(chooserIntent);
}