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.
        }
    });