如何检查是否用户登录与FB SDK 4.0为Android?用户登录、FB、SDK、Android

2023-09-03 23:04:36 作者:℡黑与白的对比。

前几天我实现FB登录我的应用程序,今天我发现,大部分我所实现的事已至此pcated德$ P $。

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.

在,我是用会话来查看是否已登录或没有用户。但是,不与新的SDK工作

Before, I was using Session to see if the user was logged in or not. However, that doesn't work with the new SDK.

根据他们的文档,我们可以使用 AccessToken.getCurrentAccessToken() Profile.getCurrentProfile()检查如果已经登录的用户,但是我不能让利用者。

According to their docs, we can use AccessToken.getCurrentAccessToken() and Profile.getCurrentProfile() to check if the user is already logged in, but I could not make use of those.

我想是这样的:

if(AccessToken.getCurrentAccessToken() == null)

我不知道这是否会工作,如果我能的这里面使用它(这也是由FB提供):

I wonder if that would work if I could use it inside of this (which is also provided by FB):

LoginManager.getInstance().registerCallback(callbackManager, new LoginManager.Callback() {...});

不过,我得到一个无法解析符号'回调'。

修改!!!!!!

好了,所以我可以检查,如果用户登录使用以下内容:

Alright, so I was able to check if the user is logged in by using the following:

开的onCreate:的

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

那么,卡列斯我的 updateWithToken 方法:

private void updateWithToken(AccessToken currentAccessToken) {
    if (currentAccessToken != null) {

            LOAD ACTIVITY A!

    } else {

            LOAD ACTIVITY B!
    }
}

现在的问题是:如果用户使用的应用程序和登录不是招之前,我可以检查的!但是,如果它是第一次用户正在使用的应用程序, updateWithToken 它永远不会被我AccessTokenTracker。

Now, the problem is: If the user has used the application and hasn logged in before, I can check for that! But if it is the first time that the user is using the app, updateWithToken is never called by my AccessTokenTracker.

我真的AP preciate如果有人可以帮助。

I'd really appreciate if someone could help.

谢谢!

推荐答案

我知道了!

首先,确保你已经初始化您的FB SDK。其次,添加以下内容:

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

这个时候有对当前访问托克斯(Tokes)的变化将被调用。意思是,这只会帮助你,如果已经登录的用户在

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.

接下来,我们将它添加到我们的的onCreate()方法:

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

updateWithToken(AccessToken.getCurrentAccessToken());

然后,当然,我们的 updateWithToken()方法:

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

这是为我做! =]