Created
October 26, 2011 13:29
-
-
Save gipi/1316346 to your computer and use it in GitHub Desktop.
Simple example of facebook posting in Android
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.facebook.android.*; | |
import com.facebook.android.Facebook.*; | |
public class MyActivity extends Activity { | |
private Facebook mFacebook = new Facebook("0123456", R.drawable.ic_fb); | |
public void callFacebook(final Activity activity, final String title, final String caption, final String picURL) { | |
android.util.Log.i("FB", "callFacebook"); | |
mFacebook.authorize(activity, new DialogListener() { | |
@Override | |
// http://stackoverflow.com/questions/2953146/android-java-post-simple-text-to-facebook-wall | |
public void onComplete(Bundle values) { | |
android.util.Log.i("#### FB", "onComplete"); | |
if (values.isEmpty()) { | |
//"skip" clicked ? | |
return; | |
} | |
// if facebookClient.authorize(...) was successful, this runs | |
// this also runs after successful post | |
// after posting, "post_id" is added to the values bundle | |
// I use that to differentiate between a call from | |
// faceBook.authorize(...) and a call from a successful post | |
// is there a better way of doing this? | |
if (!values.containsKey("post_id")) { | |
android.util.Log.i("#### FB", "not post_id"); | |
try { | |
Bundle parameters = new Bundle(); | |
parameters.putString("message", "this is a test");// the message to post to the wall | |
parameters.putString("caption", caption);// the message to post to the wall | |
parameters.putString("picture", picURL);// the message to post to the wall | |
parameters.putString("name", title);// the message to post to the wall | |
parameters.putString("link", "http://www.example.com");// the message to post to the wall | |
mFacebook.dialog(activity, "stream.publish", parameters, this);// "stream.publish" is an API call | |
} catch (Exception e) { | |
// TODO: handle exception | |
android.util.Log.i("#### FB", e.getMessage()); | |
} | |
} | |
} | |
@Override | |
public void onFacebookError(FacebookError error) { | |
android.util.Log.i("#### FB", "FacebookError: " + error); | |
} | |
@Override | |
public void onError(DialogError e) { | |
android.util.Log.i("#### FB", "DialogError: " + e); | |
} | |
@Override | |
public void onCancel() { | |
android.util.Log.i("#### FB", "cancel"); | |
} | |
}); | |
} | |
public void onActivityResult(int requestCode, int resultCode, Intent intent) { | |
mFacebook.authorizeCallback(requestCode, resultCode, data); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Kk