将Facebook集成到我的Android应用程序中

时间:2011-01-07 08:10:10

标签: facebook android

我搜索了很多代码的完美示例,帮助我了解如何在我的应用程序中集成Facebook。我该如何整合它?

3 个答案:

答案 0 :(得分:2)

请执行以下链接中给出的步骤 facebook android integration

答案 1 :(得分:1)

答案 2 :(得分:0)

private static final String FB_KEY = "YOUR_KEY";
private Facebook facebook;
private String messageToPost;
facebook = new Facebook(FB_KEY);

if (!facebook.isSessionValid()) {
        loginAndPostToWall();
    } else {
        postToWall(messageToPost);
    }

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

public void postToWall(String message) {
    FBThread fbPost = new FBThread (message);
    fbPost.start();
}


  private class FBThread extends Thread {

    String message;

    FBThread(String message) {
        this.message = message;
    }

    @Override
    public void run() {

        Bundle parameters = new Bundle();
        parameters.putString("message", message);

            try {
            facebook.request("me");
            String response = facebook.request("me/feed", parameters,
                    "POST");

            if (response == null || response.equals("")
                    || response.equals("false")) {
                toastMessage = "Blank response.";
            } else if (response.contains("error")) {
                toastMessage = "Post Failed because of duplicates...";
            } else {
                toastMessage = "Message posted to your facebook wall!";
            }

        } catch (Exception e) {
            toastMessage = "Failed to post to wall!";
            e.printStackTrace();
        }

    }
}

    class LoginDialogListener implements DialogListener {
    public void onCancel() {

        android.webkit.CookieManager.getInstance().removeAllCookie();
    }

    public void onComplete(Bundle values) {

        if (messageToPost != null) {
            postToWall(messageToPost);
        }
    }

    public void onError(DialogError error) {

        android.webkit.CookieManager.getInstance().removeAllCookie();
    }

    public void onFacebookError(FacebookError error) {

        android.webkit.CookieManager.getInstance().removeAllCookie();
    }
}