Friday, 30 December 2011

How to Update Facebook Status from Android Application?

First Download Facebook Android SDK from github then Create New Project and Write below Code into OnClicklistener of Button.

MainActivity.java

Facebook facebook = new Facebook(APP_ID);
restoreCredentials(facebook);
messageToPost = "Hello Everyone.";
if (!facebook.isSessionValid()) {
            loginAndPostToWall();
} else {
            postToWall(messageToPost);
}


Declare below variables into MainActivity.java

private Facebook facebook;
private String messageToPost;
private static final String APP_ID = "152424051507008";
private static final String[] PERMISSIONS = new String[] { "publish_stream" };
private static final String TOKEN = "access_token";
private static final String EXPIRES = "expires_in";
private static final String KEY = "facebook-credentials";


Add below methods into your MainActivity.java

public boolean saveCredentials(Facebook facebook) {
    Editor editor = getApplicationContext().getSharedPreferences(KEY, Context.MODE_PRIVATE).edit();
    editor.putString(TOKEN, facebook.getAccessToken());
    editor.putLong(EXPIRES, facebook.getAccessExpires());
    return editor.commit();
}

public boolean restoreCredentials(Facebook facebook) {
    SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences(KEY, Context.MODE_PRIVATE);
    facebook.setAccessToken(sharedPreferences.getString(TOKEN, null));
    facebook.setAccessExpires(sharedPreferences.getLong(EXPIRES, 0));
    return facebook.isSessionValid();
}

public void loginAndPostToWall() {
    facebook.authorize(this, PERMISSIONS,Facebook.FORCE_DIALOG_AUTH, new LoginDialogListener());
}

public void postToWall(String message) {
    Bundle parameters = new Bundle();
    parameters.putString("message", message);
    parameters.putString("description", "topic share");
    try {
        facebook.request("me");
        String response = facebook.request("me/feed", parameters, "POST");
        Log.d("Tests", "got response: " + response);
        if (response == null || response.equals("") || response.equals("false")) {
            showToast("Blank response.");
        } else {
            showToast("Message posted to your facebook wall!");
        }
    } catch (Exception e) {
        showToast("Failed to post to wall!");
        e.printStackTrace();
    }
}

class LoginDialogListener implements DialogListener {
    public void onComplete(Bundle values) {
        saveCredentials(facebook);
        if (messageToPost != null) {
            postToWall(messageToPost);
        }
    }

    public void onFacebookError(FacebookError error) {
        showToast("Authentication with Facebook failed!");
    }

    public void onError(DialogError error) {
        showToast("Authentication with Facebook failed!");
    }

    public void onCancel() {
        showToast("Authentication with Facebook cancelled!");
    }
}

private void showToast(String message) {
    Toast.makeText(getApplicationContext(), message, Toast.LENGTH_SHORT)
                .show();
}


Now Run Your Project

Enjoy :-)

You can download complete source code from this link.

Android - Facebook Integration

Don’t forget to provide feedback or follow this blog, if you find this blog is useful.

18 comments:

  1. i'm getting this error:

    Failed to find provider info for com.facebook.katana.provider.AttributionIdProvider

    ReplyDelete
    Replies
    1. Please see my update and download complete source code.

      Delete
  2. Hi....
    this is my first attempt on android app with facebook integration .
    i m also getting the same error , please tell me what should i do to solve this.
    i am eagerly to solve this issue.please help me to get out of this.

    Thanks in advance

    my error as:-
    "Failed to find provider info for com.facebook.katana.provider.AttributionIdProvider"

    ReplyDelete
    Replies
    1. Hello Yogesh,

      Please Download code from above link and put your APP_ID in Main_Activity file and after that if you have any issue then give me your email address i will send you demo application.

      Delete
  3. darjidhruvit@gmail.com can you please send me a demo application i urgently require that cause i have to include this thing into project and i am having error in other code like ,,,solution for Failed to find provider info for com.facebook.katana.provider.AttributionIdProvider

    ReplyDelete
    Replies
    1. Hello Dhruvit,

      You can download source code of this post from below link.

      http://www.mediafire.com/?3n34v50c6p64r4a

      Delete
  4. Hello! How can I Extend the expiration of existing short-lived user access_tokens?

    ReplyDelete
    Replies
    1. Hello Luz,

      Sorry but i don't know about this.

      Delete
  5. Hello! Now I'm having this Error: "message": "An active access token must be used to query information about the current user". "type": "OAuthException"..

    ReplyDelete
    Replies
    1. Hello Luz,

      There is some problems in your Access_Token or APP_ID so please check it first.

      Delete
  6. Thanks it is very helpful for me.grate work.

    ReplyDelete
  7. error on LoginDialogListener
    new LoginDialogListener());
    say can't resolved

    ReplyDelete
    Replies
    1. Hello,

      You can download source code of this post from below link.

      http://www.mediafire.com/?3n34v50c6p64r4a

      Delete
  8. Thanks to Dipak Keshariya . It is good tutorial . I got a some doubt . how to integrate the code to share with text(message) and image .

    ReplyDelete
  9. hello when i click on log in facebook...it start loading...and then finish application. no option for post

    ReplyDelete
  10. success,but This application in not possible log in of another user.
    please give me idea..........

    ReplyDelete
  11. Hi

    iam not finding any source code on this link:
    http://www.mediafire.com/?3n34v50c6p64r4a

    Please provide me source code link.

    Thanks & Regards
    Pavan.

    ReplyDelete