Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sharing Content On FaceBook Android

I use intent and Action.SEND for sharing my custom message on social networks like WhatsApp , twitter, Facebook and GMail. Everything is ok on Gmail and other applications except Facebook! How can I customize my code to share something on Facebook as well? I do share on Facebook using Facebook SDK with no problem, but I want to do it using an intent.

this is what I use:

Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, knowTitle+"Read the full article via MomsApp by EnfaMama A+ at http://meadjohnsonasia.com.my/mobileapp");

sendIntent.putExtra(Intent.EXTRA_SUBJECT, "I just read "+knowTitle);
sendIntent.setType("*/*");
startActivity(Intent.createChooser(sendIntent, "Share Your Favorite Article"));
like image 970
Mahdi Giveie Avatar asked Nov 22 '12 02:11

Mahdi Giveie


3 Answers

What I did was actually to intercept the chosen target of the intenthandlers, you can do that by using your actionprovider. Let's say you created an item that with an onclick starts the intent. In order to do that, you can instantiate an actionprovider to do so. This actionprovider can have a setOnShareTargetSelectedListener to intercept any intents that you want to handle differently (or not at all ^^). See the code below for how to configure your actionprovider.

 actionProvider.setShareIntent(createShareIntent());
    actionProvider.setOnShareTargetSelectedListener(new OnShareTargetSelectedListener(){

        @Override
        public boolean onShareTargetSelected(ShareActionProvider source,
                Intent intent) {
            if ("com.facebook.katana".equals(intent.getComponent().getPackageName()) && mfacebooksharer != null) {
                mfacebooksharer.shareStatus(subject, text);
                  return true;
                }
                return false;
        }

    });

Whenever facebook is chosen, I use my mfacebooksharer to handle the intent and follow the facebook API. Ofcourse, that actionrpovider needs to have an intent. (Just like you wanted to work with an intent). I use the method below to create the intent.

 private Intent createShareIntent() {
        intentsetter.setIntentleave(true);
        Intent shareIntent = new Intent(Intent.ACTION_SEND);
        shareIntent.setType("text/plain");
        shareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject);
        shareIntent.putExtra(android.content.Intent.EXTRA_TEXT, text);          
        return shareIntent;
    }
like image 75
Jeroen Avatar answered Oct 15 '22 01:10

Jeroen


As per the Facebook's Platform Policies, you cannot pre-fill the share dialog using Intent.EXTRA_TEXT. It is usually thought to be a bug, but as per a Bug Report filed here and also, here, Facebook clearly mentions that this is not the case (it's not a bug).

You can read more about their Platform Policies specifically, Platform Policy IV.2

Quote from Platform Policy IV.2:

You must not pre-fill any of the fields associated with the following products, unless the user manually generated the content earlier in the workflow: Stream stories (user_message parameter for Facebook.streamPublish and FB.Connect.streamPublish, and message parameter for stream.publish), Photos (caption), Videos (description), Notes (title and content), Links (comment), and Jabber/XMPP.

These fields are intended for users to express themselves. Pre-filling these fields erodes the authenticity of the user voice.

The only way you can share stories from your App is by integrating the Facebook SDK, which as per your post, you are already able to successfully. That is the only option available (unfortunately).

like image 23
Siddharth Lele Avatar answered Oct 15 '22 01:10

Siddharth Lele


Using Intent in Android, you can share only a link without text:

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

It'll work. If you want to share text and link , you have to use the Facebook SDK for Android: https://github.com/facebook/facebook-android-sdk

like image 36
secretlm Avatar answered Oct 14 '22 23:10

secretlm