Android Facebook SDK:检查用户是否登录
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12402775/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android Facebook SDK: Check if the user is logged in or not
提问by Jay Sidri
I'm have a feature on my Android app where the user authorizes the app and shares a link.
我的 Android 应用程序有一项功能,用户可以在其中授权应用程序并共享链接。
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).
我还需要为用户提供一个退出 facebook 的选项,如果用户未登录(或未授权应用程序),我需要有条件地禁用此按钮。
I can't seem to find the API call on the Android SDKthat would let me ask FB if the user is logged in or not.
我似乎无法在Android SDK上找到可以让我询问 FB 用户是否已登录的 API 调用。
What I have found is getAccessExpires()
:
我发现的是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.
检索当前会话的过期时间(自 Unix 纪元以来的毫秒数),如果会话未过期或不存在,则为 0。
Will checking if the session equals 0 be the way to go? Or is there something I'm missing?
检查会话是否等于 0 是要走的路吗?或者有什么我想念的吗?
回答by Diljeet
Facebook SDK 4.x versions have a different method now:
Facebook SDK 4.x 版本现在有一个不同的方法:
boolean loggedIn = AccessToken.getCurrentAccessToken() != null;
or
或者
by using functions
通过使用函数
boolean loggedIn;
//...
loggedIn = isFacebookLoggedIn();
//...
public boolean isFacebookLoggedIn(){
return AccessToken.getCurrentAccessToken() != null;
}
Check this link for better reference https://developers.facebook.com/docs/facebook-login/androidcheck this heading too "Access Tokens and Profiles" it says "You can see if a person is already logged in by checking AccessToken.getCurrentAccessToken() and Profile.getCurrentProfile()
检查此链接以获得更好的参考https://developers.facebook.com/docs/facebook-login/android检查此标题“访问令牌和配置文件”它说“您可以通过检查 AccessToken 来查看某人是否已登录。 getCurrentAccessToken() 和 Profile.getCurrentProfile()
回答by Tony Chan
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.
我努力在 FB 文档中找到一个简单的答案。使用 Facebook SDK 3.0 版,我认为有两种方法可以检查用户是否已登录。
1) Use Session.isOpened()
1) 使用 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).
要使用此方法,您需要检索活动会话,getActiveSession()
然后(这是令人困惑的部分)解密会话是否处于用户登录的状态。我认为对登录用户来说唯一重要的是 session isOpened()
. 因此,如果会话不是null
并且它是打开的,则用户已登录。在所有其他情况下,用户已注销(请记住,Session
除了打开和关闭之外,还可以有其他状态)。
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
2) 用Session.StatusCallback
和持续监控状态变化UiLifecycleHelper
If you follow this tutorialyou'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:
如果您遵循本教程,您将在实例化时设置UiLifecycleHelper
并注册一个Session.StatusCallback
对象。有一个回调方法,call()
您可以重写Session.StatusCallback
该方法,在用户登录/退出时应该会调用该方法。在该方法中,您也许可以跟踪用户是否已登录。也许是这样的:
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 1is simpler and probably the better choice.
我认为方法1更简单,可能是更好的选择。
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).
作为旁注,任何人都可以阐明为什么教程喜欢调用state.isOpened()
而不是session.isOpened()
因为两者似乎可以互换(session.isOpened()
似乎只是调用state
版本)。
回答by Jesse Chen
Note to readers:This is now deprecated in the new FB 3.0 SDK.
读者注意事项:现在在新的 FB 3.0 SDK 中已弃用。
facebook.isSessionValid()
returns true if user is logged in, false if not.
facebook.isSessionValid()
如果用户已登录,则返回 true,否则返回 false。
回答by Zennichimaro
Session.getActiveSession().isOpened()
returns true if user is logged in, false if not
如果用户已登录,则返回 true,否则返回 false
回答by Son Nguyen Thanh
Android Studio with :
Android Studio 具有:
compile 'com.facebook.android:facebook-android-sdk:4.0.1'
then check login like as:
然后像这样检查登录:
private void facebookPost() {
//check login
AccessToken accessToken = AccessToken.getCurrentAccessToken();
if (accessToken == null) {
Log.d(TAG, ">>>" + "Signed Out");
} else {
Log.d(TAG, ">>>" + "Signed In");
}
}
回答by leeCoder
@diljeet was right. https://stackoverflow.com/a/29375963/859330
@diljeet 是对的。https://stackoverflow.com/a/29375963/859330
In addition, use
此外,使用
return AccessToken.getAccessToken() != null && Profile.getCurrentProfile()!=null;
return AccessToken.getAccessToken() != null && Profile.getCurrentProfile()!=null;
It always works this way.
它总是以这种方式工作。
回答by Alberto Méndez
For Facebook Android SDK 4.x you have to use the "AccessToken.getCurrentAccessToken()" as said by @Diljeet but his check didn't work for me, I finally checked it by doing:
对于 Facebook Android SDK 4.x,您必须使用@Diljeet 所说的“AccessToken.getCurrentAccessToken()”,但他的支票对我不起作用,我最终通过以下方式进行了检查:
Activity "onCreate":
活动“onCreate”:
facebookAccessToken = AccessToken.getCurrentAccessToken();
To check if the session is still active (I made it in the "onResume" method but do it where you need):
要检查会话是否仍然处于活动状态(我在“onResume”方法中进行了但在您需要的地方进行):
if(facebookAccessToken != null){
sessionExpired = facebookAccessToken.isExpired();
}else{
sessionExpired = true;
}
More info in https://developers.facebook.com/docs/facebook-login/android
https://developers.facebook.com/docs/facebook-login/android 中的更多信息
回答by Beyaz
I was using FB sdk for just for login.. Facebook developers reject my app, Because whenever user login, I send logout.. I dont matter user profile data... I just use FB for login.. FB tells if user login... dont send lgout...
我使用 FB sdk 仅用于登录.. Facebook 开发人员拒绝我的应用程序,因为每当用户登录时,我都会发送注销.. 我不关心用户个人资料数据......我只是使用 FB 登录.. FB 告诉用户是否登录。 .. 不要发送 lgout ......
I find Hacking way...
Now my app doesnot send logout whenever user login.... Whatever user login with,Google,Facebook,or normal.. When they click logout... In this three condition
I use
LoginManager.getInstance().logOut();
我找到了黑客方式......现在我的应用程序不会在用户登录时发送注销......无论用户登录,谷歌,Facebook还是正常......当他们点击注销......在这三种情况下我使用
LoginManager.getInstance().logOut();
No matter which platform they use... There is no crash :)
无论他们使用哪个平台......都没有崩溃:)
回答by Thomas L.
I had the same issue. Here is my solution using SDK 4.0:
我遇到过同样的问题。这是我使用 SDK 4.0 的解决方案:
First of all, in your activity dealing with login check, be sure to call this primary:
首先,在你处理登录检查的活动中,一定要调用这个主要的:
FacebookSdk.sdkInitialize(this.getApplicationContext());
In your onCreate method put this :
在你的 onCreate 方法中放这个:
updateWithToken(AccessToken.getCurrentAccessToken());
new AccessTokenTracker() {
@Override
protected void onCurrentAccessTokenChanged(AccessToken oldAccessToken, AccessToken newAccessToken) {
updateWithToken(newAccessToken, handler);
}
};
Then add the method called:
然后添加调用的方法:
private void updateWithToken(AccessToken currentAccessToken) {
if (currentAccessToken != null) {
fillUIWithFacebookInfos(handler);
} else {
login();
}
}
This way will handle the already logged in user and the newly logged in user.
这种方式将处理已经登录的用户和新登录的用户。
回答by DeaMon1
This seems to be working quite well with the new sdk.
这似乎与新的 sdk 配合得很好。
private boolean isFacebookLoggedIn(){
Session session = Session.getActiveSession();
if (session != null) {
//Session can be open, check for valid token
if (!session.isClosed()) {
if(!session.getAccessToken().equalsIgnoreCase("")){
return true;
}
}
}
return false;
}