7

I cannot find many examples of the FB + Android SDK and the samples are not simple enough(some of them are deprecated). My simple goal is to share some content on FB using my Android app. When I developed the iOS app it was simply

AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
[appDelegate facebookLogin];


NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                               @"pikSpeak", @"name",
                               shareURL, @"link",
                               @"pikSpeak for iPhone !", @"caption",
                               @"Record audio at the moment of the image taken using pikSpeak and feel the moment come alive", @"description",
                               @"shared an audible pic using pikSpeak cam", @"message",
                               imageURL,@"picture",
                               nil];

[[appDelegate facebook] requestWithGraphPath:@"feed" andParams:params andHttpMethod:@"POST" andDelegate:self];

This code handled the sessions and saving the session details over app restarts.

1) How to simply share some thing in Android.

2) I saw Facebook(String app_id) is deprecated. If so, then what is its replacement?

P.S. : Using Facebook 3.0 SDK

dakshbhatt21
  • 3,558
  • 3
  • 31
  • 40
rahulg
  • 2,183
  • 3
  • 33
  • 47
  • Are you willing to use the Facebook app on the user's phone to share that content? If so, doing it on Android will be much easier than on iOS. Which versions of Android are you targeting? – Stephan Branczyk Mar 30 '13 at 05:09
  • 1
    The replacement for the `Facebook` class is mostly the `Session` class in Facebook 3.0 SDK. See this tutorial: https://developers.facebook.com/docs/tutorials/androidsdk/3.0/scrumptious/publish-open-graph-story/ – Tushar Mar 30 '13 at 05:15
  • @StephanBranczyk Am building the app for Gingerbread (2.3.3) and above versions. Is it different for different versions? – rahulg Mar 30 '13 at 06:33

2 Answers2

17

Taken from Share on User's Wall using Facebook SDK:

private void share() {
    Bundle bundle = new Bundle();
    bundle.putString("caption", "Harlem Shake Launcher for Android");
    bundle.putString("description", "Your android can do the Harlem Shake. Download it from google play");
    bundle.putString("link", "https://play.google.com/store/apps/details?id=mobi.shush.harlemlauncher");
    bundle.putString("name", "Harlem Shake Launcher");
    bundle.putString("picture", "http://shush.mobi/bla.png");
    new WebDialog.FeedDialogBuilder(mContext, mySession, bundle).build().show();
}

If you need to login (add this in you activity wherever you need to login/share):

Session.openActiveSession(this, true, new Session.StatusCallback() {

    @Override
    public void call(Session session, SessionState state, Exception exception) {
        if(session.isOpened()) {
            share();
        }
    }
});

You will need to add to your activity:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    switch(requestCode) {
    default:
        if(Session.getActiveSession() != null) //I need to check if this null just to sleep peacefully at night
            Session.getActiveSession().onActivityResult(this, requestCode, resultCode, data);
        break;
    }
}
Sherif elKhatib
  • 45,786
  • 16
  • 89
  • 106
  • **I need to check if this null just to sleep peacefully at night** then when some one will get bug you will keep sleeping. Me, by avoiding null check in the places when I don't believe they should be, I am waking up when some one have a bug and fixing it. Then when I sleep well at night I know for sure so is every one else. – Ilya Gazman Apr 26 '13 at 10:41
  • Perfect - after trudging through lots of dead ends and false starts online (probably due to older/newer FB APK's), this is the one that worked ! I can't use the modern FB Share classes because I want to be compatible from Android 2.3 upwards, so I need to use FB SDK 3.17... –  Feb 02 '17 at 23:14
0

Feed Dialog is used for posting on your own wall, Technically speaking it is not sharing. Also, if you wish to achieve the share functionality, go for the share dialog provided by facebook sdk for android. The facebook Share Dialog provides various functions to tag friends, places etc. making use of the OpenGraphAction.

Kailas
  • 7,350
  • 3
  • 47
  • 63