Android Facebook SDK:检查用户是否登录 [英] Android Facebook SDK: Check if the user is logged in or not
问题描述
我的Android应用程式中有一项功能,用户授权该应用程式并共享一个链接。
我还需要为用户提供一个选项退出Facebook,如果用户没有登录int(或没有授权应用程序),我需要有条件地禁用此按钮。
我似乎找不到API请致电 Android SDK ,让我询问FB用户是登录还是
我发现是 getAccessExpires()
:
检索当前会话的到期时间(以
Unix epoch为单位毫秒),如果会话不过期或不存在,则为0。
会检查会话是否等于0是否要走?还是有一些我错过的东西?
我在FB文档中很难找到一个简单的答案。使用Facebook SDK 3.0版我认为有两种方法来检查用户是否登录。
1)使用会话。 isOpened()
要使用此方法,您需要使用 getActiveSession()
然后(这是令人困惑的部分)如果会话处于用户登录状态,则会进行解密。我认为唯一对登录用户来说重要的是会话 isOpened()
。因此,如果会话不是 null
并且它是打开的,那么用户登录。在所有其他情况下,用户已注销(请记住
public boolean isLoggedIn(){
Session session = Session.getActiveSession();
return(session!= null&& session.isOpened());
}
还有另一种方式来写这个函数,详细的在这个 answer ,但我不知道哪种方法更清晰或最佳实践。
2)使用
Session.StatusCallback
和持续监视状态更改UiLifecycleHelper
如果你遵循这个教程,您将设置 UiLifecycleHelper
并注册一个 Session.StatusCallback
对象实例化。有一个回调方法, call()
,您可以在 Session.StatusCallback
中覆盖,这应该在用户随时被调用登录/登出在该方法中,您可以跟踪用户是否登录。可能是这样的:
private boolean isLoggedIn = false; //默认假设没有登录
private Session.StatusCallback callback = new Session.StatusCallback(){
@Override
public void call(Session session,SessionState state,Exception例外){
if(state.isOpened()){//注意:我认为session.isOpened()是一样的
isLoggedIn = true;
} else if(state.isClosed()){
isLoggedIn = false;
}
}
};
public boolean isLoggedIn(){
return 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屋!