How to check if user is logged in with FB SDK 4.0 for Android?

前端 未结 6 1765
走了就别回头了
走了就别回头了 2020-11-30 18:56

A few days ago I implemented FB Login to my APP, and today I found out that most of the things I have implemented are now deprecated.

Before, I was using Sess

6条回答
  •  刺人心
    刺人心 (楼主)
    2020-11-30 19:18

    You can use the same way Felipe mentioned in his answer or you can use the other two ways. But it seems AccessTokenTracker is the convenient way, since it helps you to track the access tokens (use with ProfileTracker class)

    1. If you are using a custom button for your login use LoginManager call back

    For example

    In your layout xml

        

    In your Activity

        //Custom Button
        Button myFacebookButton = (Button) findViewById(R.id.my_facebook_button);
    

    The button onclick Listener

    public void facebookLogin(View view) {
            LoginManager.getInstance().logInWithReadPermissions(this, Arrays.asList("public_profile", "user_friends"));
        }
    

    At the end the LoginManager Callback

     //Create callback manager to handle login response
            CallbackManager callbackManager = CallbackManager.Factory.create();
    
           LoginManager.getInstance().registerCallback(callbackManager, new FacebookCallback() {
               @Override
               public void onSuccess(LoginResult loginResult) {
                   Log.i(TAG, "LoginManager FacebookCallback onSuccess");
                   if(loginResult.getAccessToken() != null) {
                       Log.i(TAG, "Access Token:: " + loginResult.getAccessToken());
                       facebookSuccess();
                   }
               }
    
               @Override
               public void onCancel() {
                   Log.i(TAG, "LoginManager FacebookCallback onCancel");
               }
    
               @Override
               public void onError(FacebookException e) {
                   Log.i(TAG, "LoginManager FacebookCallback onError");
               }
           });
    
    1. If you are using the button (com.facebook.login.widget.LoginButton) provided in SDK use LoginButton callback (This is crealy detailed in their reference doc - https://developers.facebook.com/docs/facebook-login/android/v2.3)

    For example

    In your layout xml

    
    

    In your activity

        //Facebook SDK provided LoginButton
        LoginButton loginButton = (LoginButton) findViewById(R.id.login_button);
        loginButton.setReadPermissions("user_friends");
        //Callback registration
        loginButton.registerCallback(callbackManager, new FacebookCallback() {
            @Override
            public void onSuccess(LoginResult loginResult) {
                // App code
                Log.i(TAG, "LoginButton FacebookCallback onSuccess");
                if(loginResult.getAccessToken() != null){
                    Log.i(TAG, "Access Token:: "+loginResult.getAccessToken());
                    facebookSuccess();
                }
    
            }
    
            @Override
            public void onCancel() {
                // App code
                Log.i(TAG, "LoginButton FacebookCallback onCancel");
            }
    
            @Override
            public void onError(FacebookException exception) {
                // App code
                Log.i(TAG, "LoginButton FacebookCallback onError:: "+exception.getMessage());
                Log.i(TAG,"Exception:: "+exception.getStackTrace());
            }
        });
    

    Dont forget to call callbackManager.onActivityResult(requestCode, resultCode, data); in your Activity onActivityResult()

提交回复
热议问题