92

I have an Android app and it supports sending text via other apps. It therefore uses the ACTION_SEND intent and the EXTRA_TEXT field. The chooser presents me with all apps that can handle such an intent. Those are Twitter, Email, ... and Facebook. But when I select Facebook it opens the browser and goes to the following page:

http://m.facebook.com/sharer.php?u=mytext

It shows my text and the submit button. But when I press the submit button nothing happens. The page just loads again. I think maybe it is only possible to send URLs via the Facebook App. Could that be?

Did anyone manage to send text via ACTION_SEND through the Facebook Android app?

Bugs Happen
  • 2,169
  • 4
  • 33
  • 59
Goddchen
  • 4,459
  • 4
  • 33
  • 54
  • 4
    If you visit this SO post and are frustrated that the facebook app doesn't properly support ACTION_SEND then please take the time to add your post to the following topic: http://forum.developers.facebook.net/viewtopic.php?id=93900 – Error 454 Mar 23 '11 at 20:54
  • @Error 454:I have posted at facebook but i am curious how the share option in the gallery still works?check out my question http://stackoverflow.com/questions/5692583/post-on-facebook-wall-in-android – rogerstone Apr 17 '11 at 14:14
  • Facebook's Eric Tseng confirmed to me on Twitter today that they're looking to fix this. Here's hoping the next release of the app correctly accepts shared text. – Ollie C May 11 '11 at 12:50
  • I have this problem too, FB android v 1.7.2 – Paweł Oct 10 '11 at 14:25
  • 7
    I´m guessing 21-3-2012 still not solved? Because I can´t get it to work.. – Diego Mar 21 '12 at 12:37
  • June 24, 2012 - still not solved by Facebook nor Google – Dominik Jun 24 '12 at 00:24
  • 1
    https://developers.facebook.com/bugs/332619626816423 - Looks like the facebook design team have closed this one as pre-filling a message is against their policy :( – Brett Feb 21 '13 at 10:13
  • 1
    @Brett That is freaking ridiculous. They just want to make you add their SDK to your project. – theblang Mar 03 '14 at 22:47
  • 2
    May 17, 2015 - still not working with facebook. – Faisal Asif May 17 '15 at 12:53
  • how is this still an issue? why Facebook does not allow this method? – Rain Man Aug 18 '15 at 18:22

10 Answers10

51

To make the Share work with the facebook app, you only need to have suply at least one link:

Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TEXT, "Wonderful search engine http://www.google.fr/");
startActivity(Intent.createChooser(intent, "Share with"));

This will show the correct sharing window but when you click on share, nothing happends (I also tried with the official Twitter App, it does not work).

The only way I found to make the Facebook app sharing work is to share only a link with no text:

Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TEXT, "http://www.google.fr/");
startActivity(Intent.createChooser(intent, "Share with"));

It will show the following window and the Share button will work:

facebook share

Apparently it automatically takes an image and text from the link to populate the share.

If you want to share only text, you will have to use the facebook api: https://github.com/facebook/facebook-android-sdk

ol_v_er
  • 27,094
  • 6
  • 48
  • 61
  • 4
    Where is the "Wonderful search engine" text? That's the same bug people are reporting above, ya? – Kyle Clegg Mar 21 '12 at 22:28
  • 3
    He's saying you can only post a link. No text. The "Wonderful search engine" text had to be taken out in order for it to work. – chubbsondubs Jul 16 '12 at 19:10
  • I will +1 if you update the answer. As of now the `Facebook` app will correctly pull the link from a string like `Wonderful search engine http://www.google.fr/`. Note that I said **pull the link**, it still won't do anything with the text. Also, `Twitter` works with both the text and the link. – theblang Mar 04 '14 at 15:30
46

06/2013 :

  • This is a bug from Facebook, not your code
  • Facebook will NOT fix this bug, they say it is "by design" that they broke the Android share system : https://developers.facebook.com/bugs/332619626816423
  • use the SDK or share only URL.
  • Tips: you could cheat a little using the web page title as text for the post.
Loda
  • 1,970
  • 2
  • 20
  • 40
29

First you need query Intent to handler sharing option. Then use package name to filter Intent then we will have only one Intent that handler sharing option!

Share via Facebook

Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
shareIntent.setType("text/plain");
shareIntent.putExtra(android.content.Intent.EXTRA_TEXT, "Content to share");
PackageManager pm = v.getContext().getPackageManager();
List<ResolveInfo> activityList = pm.queryIntentActivities(shareIntent, 0);
for (final ResolveInfo app : activityList) {
    if ((app.activityInfo.name).contains("facebook")) {
        final ActivityInfo activity = app.activityInfo;
        final ComponentName name = new ComponentName(activity.applicationInfo.packageName, activity.name);
        shareIntent.addCategory(Intent.CATEGORY_LAUNCHER);
        shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |             Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
        shareIntent.setComponent(name);
        v.getContext().startActivity(shareIntent);
        break;
   }
}

Bonus - Share via Twitter

Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
shareIntent.setType("text/plain");
shareIntent.putExtra(android.content.Intent.EXTRA_TEXT, "Content to share");
PackageManager pm = v.getContext().getPackageManager();
List<ResolveInfo> activityList = pm.queryIntentActivities(shareIntent, 0);
for (final ResolveInfo app : activityList) {
    if ("com.twitter.android.PostActivity".equals(app.activityInfo.name)) {
        final ActivityInfo activity = app.activityInfo;
        final ComponentName name = new ComponentName(activity.applicationInfo.packageName, activity.name);
        shareIntent.addCategory(Intent.CATEGORY_LAUNCHER);
        shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |             Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
        shareIntent.setComponent(name);
        v.getContext().startActivity(shareIntent);
        break;
   }
}

And if you want to find how to share via another sharing application, find it there Tép Blog - Advance share via Android

dumbfingers
  • 7,001
  • 5
  • 54
  • 80
Khai Nguyen
  • 3,065
  • 1
  • 31
  • 24
  • 14
    Using the ways you still can't share text (just link) in facebook and twitter. You must use SDK Facebook (Twitter) for Android. – secretlm May 06 '12 at 13:36
  • Hello, I tried this, but it didnt shows the message that I want to Share it on facebook... Any idea why? It asks me to login and shows "Whats is in your mind?" hint text not the actual text that I want to share it – Ramesh Sangili Feb 13 '13 at 16:59
  • This doesn't work. It shows the same empty input as with the normal (dialog) intent. – User Mar 04 '13 at 13:10
  • 3
    but on twitter text are easily posted – Trikaldarshiii Jul 15 '13 at 17:26
  • 1
    Does not work for Facebook, and to enable sharing on Twitter there's no need to set the intent component. – Juozas Kontvainis Jul 16 '14 at 14:47
11

So I have a work around, but it assumes you have control over the page you're sharing...

If you format your EXTRA_TEXT like so...

String myText = "Hey!\nThis is a neat pic!";
String extraText = "http://www.example.com/myPicPage.html?extraText=\n\n" + myText;

... then on non-Facebook apps, your text should appear something like this:

http://www.example.com/myPicPage.html?extraText=

Hey!
This is a neat pic!

Now if you update your website such that requests with the extraText query parameter return the contents of extraText in the page's meta data.

<!-- Make sure to sanitize your inputs! e.g. http://xkcd.com/327/ -->
<meta name="title" content="Hey! this is a neat pic!">

Then when Facebook escapes that url to generate the dialog, it'll read the title meta data and embed it into your share dialog.

I realize this is a pretty yuck solution, so take with a grain of salt...

Cœur
  • 37,241
  • 25
  • 195
  • 267
Joseph Lee
  • 191
  • 1
  • 6
11

EDITED: with the new release of the official Facebook app for Android (July 14 2011) IT WORKS!!!

OLD: The examples above do not work if the user chooses the Facebook app for sharing, but they do work if the user chooses the Seesmic app to post to Facebook. I guess Seesmic have a better implementation of the Facebook API than Facebook!

Giulio Prisco
  • 941
  • 9
  • 16
  • In Facebook version 1.6.1 its not working, and some additional bugs seems to be there while sending ! – sat Jul 26 '11 at 18:26
  • It started working! Don't know what was the problem. Thanks it works with FB v 1.6.1 too. – sat Jul 28 '11 at 17:00
  • 9
    This answer is wrong since the release of the version 1.6.2 of the facebook app : http://developers.facebook.com/bugs/363863587019268 – alaeri Jan 21 '13 at 22:08
2

It appears that the Facebook app handles this intent incorrectly. The most reliable way seems to be to use the Facebook API for Android.

The SDK is at this link: http://github.com/facebook/facebook-android-sdk

Under 'usage', there is this:

Display a Facebook dialog.

The SDK supports several WebView html dialogs for user interactions, such as creating a wall post. This is intended to provided quick Facebook functionality without having to implement a native Android UI and pass data to facebook directly though the APIs.

This seems like the best way to do it -- display a dialog that will post to the wall. The only issue is that they may have to log in first

HXCaine
  • 4,228
  • 3
  • 31
  • 36
  • Well that's not exactly what i was asking for. But anyway thanks for the hint. I Would need to add a seperate menu item for facebook share to make this work... – Goddchen Sep 15 '10 at 21:41
  • Yeah, I understood what you meant and it's a bitch but I've had the same problem with the ACTION_SEND intent in my application and Facebook haven't fixed their app, so this seems to be the best alternative – HXCaine Sep 15 '10 at 23:30
  • 3
    I've just done some more searching and found that there are people all over the internet with this problem and Facebook isn't helping at all. http://forum.developers.facebook.net/viewtopic.php?pid=255227 – HXCaine Sep 15 '10 at 23:51
1
Check this out : By this we can check activity results also....
// Open all sharing option for user
                    Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND); 
                    sharingIntent.setType("text/plain");                    
                    sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, ShortDesc+" from "+BusinessName);
                    sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, ShortDesc+" "+ShareURL);
                    sharingIntent.putExtra(Intent.EXTRA_TITLE, ShortDesc+" "+ShareURL);
                    startActivityForResult(Intent.createChooser(sharingIntent, "Share via"),1000);
/**
     * Get the result when we share any data to another activity 
     * */
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        switch(requestCode) {
        case 1000:
            if(resultCode == RESULT_OK)
                Toast.makeText(getApplicationContext(), "Activity 1 returned OK", Toast.LENGTH_LONG).show();
            else
                Toast.makeText(getApplicationContext(), "Activity 1 returned NOT OK", Toast.LENGTH_LONG).show();
            break;
        case 1002:
            if(resultCode == RESULT_OK)
                Toast.makeText(getApplicationContext(), "Activity 2 returned OK", Toast.LENGTH_LONG).show();
            break;
        }// end switch



    }// end onActivityResult
Alex Lockwood
  • 83,063
  • 39
  • 206
  • 250
Vinod Joshi
  • 7,696
  • 1
  • 50
  • 51
1
ShareDialog shareDialog = new ShareDialog(this);
if(ShareDialog.canShow(ShareLinkContent.class)) {

    ShareLinkContent linkContent = new ShareLinkContent.Builder().setContentTitle(strTitle).setContentDescription(strDescription)
                            .setContentUrl(Uri.parse(strNewsHtmlUrl))
                            .build();
    shareDialog.show(linkContent);

}
FelixSFD
  • 6,052
  • 10
  • 43
  • 117
0

if you want to show text put # at the begging of the message you want it will share it as Hashtag

MFQ
  • 839
  • 8
  • 14
0

It appears that it's a bug in the Facebook app that was reported in April 2011 and has still yet to be fixed by the Android Facebook developers.

The only work around for the moment is to use their SDK.

Joseph Woodward
  • 9,191
  • 5
  • 44
  • 63
  • 2
    I think they want you to use the web app to share shareable content with friends, so you can see de Ads... I'm with same problem and i will implement Facebbok sharing through facebook api. – Igor Jun 01 '12 at 06:59