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

前端 未结 6 1762
走了就别回头了
走了就别回头了 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

    I got it!

    First, make sure you have initialized your FB SDK. Second, add the following:

    accessTokenTracker = new AccessTokenTracker() {
            @Override
            protected void onCurrentAccessTokenChanged(AccessToken oldAccessToken, AccessToken newAccessToken) {
                updateWithToken(newAccessToken);
            }
        };
    

    This will be called when there's a change on the Current Access Tokes. Meaning, this will only help you if the user is already logged in.

    Next, we add this to our onCreate() method:

    updateWithToken(AccessToken.getCurrentAccessToken());
    

    Then of course, our updateWithToken() method:

    private void updateWithToken(AccessToken currentAccessToken) {
    
        if (currentAccessToken != null) {
            new Handler().postDelayed(new Runnable() {
    
                @Override
                public void run() {
                    Intent i = new Intent(SplashScreen.this, GeekTrivia.class);
                    startActivity(i);
    
                    finish();
                }
            }, SPLASH_TIME_OUT);
        } else {
            new Handler().postDelayed(new Runnable() {
    
                @Override
                public void run() {
                    Intent i = new Intent(SplashScreen.this, Login.class);
                    startActivity(i);
    
                    finish();
                }
            }, SPLASH_TIME_OUT);
        }
    }
    

    That did it for me! =]

提交回复
热议问题