如何检查用户是否使用 FB SDK 4.0 for Android 登录? [英] How to check if user is logged in with FB SDK 4.0 for Android?

查看:20
本文介绍了如何检查用户是否使用 FB SDK 4.0 for Android 登录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

前几天我在我的APP上实现了FB登录,今天我发现我实现的大部分东西现在都被弃用了.

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.

之前,我使用 Session 来查看用户是否已登录.但是,这不适用于新的 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 方法:

Then, that calles my updateWithToken method:

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.

如果有人能提供帮助,我将不胜感激.

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

当当前访问令牌发生更改时将调用此方法.意思是,这只会在用户已经登录的情况下对您有所帮助.

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

对我来说就是这样!=]

That did it for me! =]

这篇关于如何检查用户是否使用 FB SDK 4.0 for Android 登录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆