Android的Facebook的SDK:检查,如果用户登录或不 [英] Android Facebook SDK: Check if the user is logged in or not

查看:151
本文介绍了Android的Facebook的SDK:检查,如果用户登录或不的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我的Andr​​oid应用程序功能,用户授权的应用程序,并共享一个链接。

我还需要给一个选项,用户注销的facebook,我需要有条件地禁用这个按钮,如果用户没有登录INT(或未经授权的应用程序)。

我似乎无法找到关于的Andr​​oid SDK 的API调用,将让我问FB如果用户登录或没有。

我所发现的是<一个href="http://developers.facebook.com/docs/reference/androidsdk/helper/"><$c$c>getAccessExpires():

  

检索当前会话的到期时间(以毫秒为单位自   Unix纪元),或者0,如果会话不会过期或不存在。

在检查如果会话等于0是要走的路?或者是有什么我失踪?

解决方案

我在努力寻找一个简单的答案,这在FB文档。使用Facebook SDK 3.0版,我认为有两种方法来检查用户是否登录。

1)使用 Session.isOpened()

要你需要检索的活动会话与<一个使用此方法href="https://developers.facebook.com/docs/reference/android/3.0/Session#getActiveSession%28%29"><$c$c>getActiveSession()然后(这里是混淆的部分)解密会话是否在登录或没有用户的状态。我认为,重要的登录的用户的唯一的事情是,如果会话 isOpened()。因此,如果会话不是,它是开放的,那么用户已登录。在其他情况下,用户将被注销(记住会议也有状态比开启和关闭)。其它

 公共布尔isLoggedIn(){
    会话会话= Session.getActiveSession();
    返程(会话= NULL和放大器;!&安培; session.isOpened());
}
 

还有另一种方式来写这个功能,在此回答详细,但我不知道哪种方法更清除或最佳实践。

2)不断地监视与 Session.StatusCallback UiLifecycleHelper

如果您按照此教程你会设置 UiLifecycleHelper ,并在实例与它注册 Session.StatusCallback 对象。有一个回调方法,通话(),您可以在 Session.StatusCallback 覆盖,将理应在任何时候称为用户登录/退出。在该方法中,也许你可以跟踪用户是否已登录或没有。也许是这样的:

 私人布尔isLoggedIn = FALSE; //默认情况下,假设没有登录

私人Session.StatusCallback回调=新Session.StatusCallback(){
    @覆盖
    公共无效呼叫(会话的会话,SessionState会状态,例外的例外){
        如果(state.isOpened()){//注:我认为session.isOpened()是一样的
            isLoggedIn = TRUE;
        }否则如果(state.isClosed()){
            isLoggedIn = FALSE;
        }
    }
};

公共布尔isLoggedIn(){
    返回isLoggedIn;
}
 

我想办法 1 更简单,可能是更好的选择。

作为一个侧面说明任何人都可以解释为什么本教程喜欢称光 state.isOpened()而不是 session.isOpened(),因为双方似乎都可以互换( session.isOpened()似乎只是调用通过对状态的版本是这样)。

I'm have a feature on my Android app where the user authorizes the app and shares a link.

I also need to give an option for the user to logout of facebook and I need to conditionally disable this button if the user is not logged int (or not authorized the app).

I can't seem to find the API call on the Android SDK that would let me ask FB if the user is logged in or not.

What I have found is getAccessExpires():

Retrieve the current session's expiration time (in milliseconds since Unix epoch), or 0 if the session doesn't expire or doesn't exist.

Will checking if the session equals 0 be the way to go? Or is there something I'm missing?

解决方案

I struggled to find a simple answer to this in the FB docs. Using the Facebook SDK version 3.0 I think there are two ways to check if a user is logged in.

1) Use Session.isOpened()

To use this method you need to retrieve the active session with getActiveSession() and then (here's the confusing part) decipher if the session is in a state where the user is logged in or not. I think the only thing that matters for a logged in user is if the session isOpened(). So if the session is not null and it is open then the user is logged in. In all other cases the user is logged out (keep in mind Session can have states other than opened and closed).

public boolean isLoggedIn() {
    Session session = Session.getActiveSession();
    return (session != null && session.isOpened());
}

There's another way to write this function, detailed in this answer, but I'm not sure which approach is more clear or "best practice".

2) Constantly monitor status changes with Session.StatusCallback and UiLifecycleHelper

If you follow this tutorial you'll setup the UiLifecycleHelper and register a Session.StatusCallback object with it upon instantiation. There's a callback method, call(), which you override in Session.StatusCallback which will supposedly be called anytime the user logs in/out. Within that method maybe you can keep track of whether the user is logged in or not. Maybe something like this:

private boolean isLoggedIn = false; // by default assume not logged in

private Session.StatusCallback callback = new Session.StatusCallback() {
    @Override
    public void call(Session session, SessionState state, Exception exception) {
        if (state.isOpened()) { //note: I think session.isOpened() is the same
            isLoggedIn = true;
        } else if (state.isClosed()) {
            isLoggedIn = false;
        }
    }
};

public boolean isLoggedIn() {
    return isLoggedIn;
}

I think method 1 is simpler and probably the better choice.

As a side note can anyone shed light on why the tutorial likes to call state.isOpened() instead of session.isOpened() since both seem to be interchangeable (session.isOpened() seems to just call through to the state version anyway).

这篇关于Android的Facebook的SDK:检查,如果用户登录或不的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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