Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android and Facebook share intent

I'm developing an Android app and am interested to know how you can update the app user's status from within the app using Android's share intents.

Having looked through Facebook's SDK it appears that this is easy enough to do, however I'm keen to allow the user to do it via the regular Share Intent pop up window? seen here:

pop up

I have tried the usual share intent code, however this no longer appears to work for Facebook.

public void invokeShare(Activity activity, String quote, String credit) {     Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);     shareIntent.setType("text/plain");     shareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, activity.getString(R.string.share_subject));     shareIntent.putExtra(android.content.Intent.EXTRA_TEXT, "Example text");          activity.startActivity(Intent.createChooser(shareIntent, activity.getString(R.string.share_title))); } 

UPDATE: Having done more digging, it looks as though it's a bug with Facebook's app that has yet to be resolved! (facebook bug) For the mean time it looks like I'm just going to have to put up with the negative "Sharing doesn't work!!!" reviews. Cheers Facebook :*(

like image 739
Joseph Woodward Avatar asked Sep 25 '11 11:09

Joseph Woodward


People also ask

What is Share intent android?

Intent class to send data from one activity to another and from current activity to outside the application. Intent class needs to specify the data and its type which is to be share. Most commonly, ACTION_SEND action sends URL of build-in Browser app.


2 Answers

Apparently Facebook no longer (as of 2014) allows you to customise the sharing screen, no matter if you are just opening sharer.php URL or using Android intents in more specialised ways. See for example these answers:

  • "Sharer.php no longer allows you to customize."
  • "You need to use Facebook Android SDK or Easy Facebook Android SDK to share."

Anyway, using plain Intents, you can still share a URL, but not any default text with it, as billynomates commented. (Also, if you have no URL to share, just launching Facebook app with empty "Write Post" (i.e. status update) dialog is equally easy; use the code below but leave out EXTRA_TEXT.)

Here's the best solution I've found that does not involve using any Facebook SDKs.

This code opens the official Facebook app directly if it's installed, and otherwise falls back to opening sharer.php in a browser. (Most of the other solutions in this question bring up a huge "Complete action using…" dialog which isn't optimal at all!)

String urlToShare = "https://stackoverflow.com/questions/7545254"; Intent intent = new Intent(Intent.ACTION_SEND); intent.setType("text/plain"); // intent.putExtra(Intent.EXTRA_SUBJECT, "Foo bar"); // NB: has no effect! intent.putExtra(Intent.EXTRA_TEXT, urlToShare);  // See if official Facebook app is found boolean facebookAppFound = false; List<ResolveInfo> matches = getPackageManager().queryIntentActivities(intent, 0); for (ResolveInfo info : matches) {     if (info.activityInfo.packageName.toLowerCase().startsWith("com.facebook.katana")) {         intent.setPackage(info.activityInfo.packageName);         facebookAppFound = true;         break;     } }  // As fallback, launch sharer.php in a browser if (!facebookAppFound) {     String sharerUrl = "https://www.facebook.com/sharer/sharer.php?u=" + urlToShare;     intent = new Intent(Intent.ACTION_VIEW, Uri.parse(sharerUrl)); }  startActivity(intent); 

(Regarding the com.facebook.katana package name, see MatheusJardimB's comment.)

The result looks like this on my Nexus 7 (Android 4.4) with Facebook app installed:

enter image description here

like image 113
Jonik Avatar answered Oct 11 '22 12:10

Jonik


The Facebook application does not handle either the EXTRA_SUBJECT or EXTRA_TEXT fields.

Here is bug link: https://developers.facebook.com/bugs/332619626816423

Thanks @billynomates:

The thing is, if you put a URL in the EXTRA_TEXT field, it does work. It's like they're intentionally stripping out any text.

like image 44
Göksel Güren Avatar answered Oct 11 '22 10:10

Göksel Güren