Facebook loginsignup 实施的简约指南

  1. 你必须设置先决条件

  2. 将 Facebook 活动添加到 AndroidManifest.xml 文件:

    <activity 
        android:name="com.facebook.FacebookActivity"
        android:configChanges= "keyboard|keyboardHidden|screenLayout|screenSize|orientation"
        android:theme="@android:style/Theme.Translucent.NoTitleBar"
        android:label="@string/app_name" />
    
  3. 将登录按钮添加到布局 XML 文件:

    <com.facebook.login.widget.LoginButton
        android:id="@+id/login_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />   
    
  4. 现在你有了 Facebook 按钮。如果用户点击它,Facebook 登录对话框将出现在应用程序的屏幕上。在这里,用户可以填写他们的凭据并按“ 登录” 按钮。如果凭据正确,则对话框将授予相应的权限,并且会向包含该按钮的原始活动发送回调。以下代码显示了如何接收回调:

    loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
        @Override
        public void onSuccess(LoginResult loginResult) {
            // Completed without error. You might want to use the retrieved data here.
        }
    
        @Override
        public void onCancel() {
            // The user either cancelled the Facebook login process or didn't authorize the app.
        }
    
        @Override
        public void onError(FacebookException exception) {
            // The dialog was closed with an error. The exception will help you recognize what exactly went wrong.
        }
    });