Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android share intent for facebook- share text AND link

I am trying to use the Android share intent to post something on facebook. It looks like this:

shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); shareIntent.setType("text/plain"); shareIntent.putExtra(android.content.Intent.EXTRA_TEXT, "Free education for all! http://linkd.in/xU8mCc"); startActivity(shareIntent); 

So my post has both - some text and a link. But when the message is posted on facebook, it only has the link, no message. I tried various extras but nothing works.

Anyone faced this issue and solved it? I have facebook app version 1.8.1

Edit: I tried removing the link, and the facebook app does not take my message (shows a blank message to be posted), but not the other way round. So looks like the app is totally ignoring any plain text messages. I am spooked! Is this a major bug in the fb app that text messages can not be posted at all (with share intent)?

like image 224
GreenBee Avatar asked Jan 07 '12 16:01

GreenBee


People also ask

What type of intent do you need when you need to share a text to another application?

When you construct an intent, you must specify the action you want the intent to perform. Android uses the action ACTION_SEND to send data from one activity to another, even across process boundaries. You need to specify the data and its type.


2 Answers

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);     } } 
like image 101
MatheusJardimB Avatar answered Sep 17 '22 16:09

MatheusJardimB


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;     } } 
like image 39
zonabi Avatar answered Sep 18 '22 16:09

zonabi