1. 接入Facebook ,首先要先下载Facebook SDK,地址(https://developers.facebook.com/docs/android?locale=zh_CN)

  1. 下载一个他的示例demo,查看其中的主要的类和方法的使用

  2. 下面我新建了一个Android项目实现了 Facebook 登录功能

    a) 首先导入Facebooksdk ,

    b) 新建一个Android工程,引入这个sdk

    c) AndroidManifest.xml 里面将以下内容复制进去

<!-- beigin、、、、、 FaceBook 的各个配置 -->
       <!-- 应用的 app_id  开发者申请的 -->
        <meta-data
            android:name="com.facebook.sdk.ApplicationId"
            android:value="@string/app_id" />
 
       <!-- FaceBook的 登录界面 -->
        <activity
            android:name="com.facebook.LoginActivity"
            android:label="@string/app_name"
            android:theme="@android:style/Theme.Translucent.NoTitleBar" />
       <!-- end、、、、、 FaceBook 的各个配置 -->


这其中包含了你申请的 app_id FacebookSdk中的登录使用的Activity

  1. 将申请到的appId放到string.xml

<string name="app_id">355198514515820</string>
  1. 新建一个Activity,也可以在MainActivity里面添加一个布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:andro
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <com.facebook.widget.LoginButton
        android:
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true" />
</RelativeLayout>


这里面包含一个Facebook SDK里面已经定义好的 登录按钮

  1. 回到Activity界面,添加以下代码

private LoginButton loginButton = null;
private UiLifecycleHelper uiHelper = null;
   @Override
    protected void onResume() {
        super.onResume();
 
        uiHelper.onResume();
    }
 
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        uiHelper.onActivityResult(requestCode, resultCode, data);
    }
 
    @Override
    protected void onPause() {
        super.onPause();
        uiHelper.onPause();
    }
 
    @Override
    protected void onDestroy() {
        super.onDestroy();
        uiHelper.onDestroy();
}


  1. g) OnCreate方法里面初始化一下Facebook的几个方法

loginButton = (LoginButton) this.findViewById(R.id.loginButton);
 
    // 设置权限
        loginButton.setReadPermissions(permissions);
/** 初始化 */
    private void initFacebook(Bundle savedInstanceState) {
        uiHelper = new UiLifecycleHelper(this, callback);
        uiHelper.onCreate(savedInstanceState);
    }
    StatusCallback callback = new Session.StatusCallback() {
        // Session 必须在 Request 之前 打开,
        @Override
        public void call(Session session, SessionState state, Exception exception) {
            onSessionStateChange(session, state, exception);
        }
    };
private void onSessionStateChange(Session session, SessionState state, Exception exception) {
 
        if (state.isOpened()) {
            currentSession = session;
            fetchUserInfo();
        }
    }
/** 抓取 用户信息 */
    private void fetchUserInfo() {
        if (currentSession != null && currentSession.isOpened()) {
            Request request = Request.newMeRequest(currentSession,
                    new Request.GraphUserCallback() {
 
                        @Override
                        public void onCompleted(GraphUser user,
                                Response response) {
                            if (response.getRequest().getSession() == currentSession) {
 
                                Log.e("First Name", user.getFirstName());
                                Log.e("Last Name", user.getLastName());
                                Log.e("id ", user.getId());
                                Log.e("User Name", "" + user.getUsername());
                                Log.e("Inner JSONObject", "" + user.getInnerJSONObject().toString());
                                Log.e("Birthday", "" + user.getBirthday());
                                Log.e("Link", "" + user.getLink());
                                Log.e("Name", "" + user.getName());
                                Log.e("property", "" + user.getProperty("name"));
                                
                            }
                        }
 
                    });
            request.executeAsync();
        }
 }


如果感觉有些乱的话,可以参看工程:http://download.csdn.net/detail/yingshetou123/8416481