如何检查用户是否使用FB SDK 4.0 for Androidlogin?

前几天我实现了FBlogin到我的APP,今天我发现,我已经实现的大部分东西已经被弃用了。

之前,我使用Session来查看用户是否已login。 但是,这不适用于新的SDK。

根据他们的文档,我们可以使用AccessToken.getCurrentAccessToken()Profile.getCurrentProfile()来检查用户是否已经login,但我无法使用这些。

我尝试了这样的事情:

 if(AccessToken.getCurrentAccessToken() == null) 

我想知道如果我可以在里面使用它(这也是由FB提供的),那是否会起作用:

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

但是,我收到“无法parsing符号”callback“”。

编辑!!!!!!

好的,所以我能够通过使用以下来检查用户是否login:

onCreate:

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

然后,这个calles我的updateWithToken方法:

 private void updateWithToken(AccessToken currentAccessToken) { if (currentAccessToken != null) { LOAD ACTIVITY A! } else { LOAD ACTIVITY B! } } 

现在,问题是:如果用户使用了应用程序,而且之前没有login,我可以检查! 但是,如果这是用户第一次使用该应用程序,我的AccessTokenTracker永远不会调用updateWithToken

如果有人能帮忙,我会很感激。

谢谢!

一个更简单的解决scheme为我的情况工作(我不知道这是否是更优雅的方式):

 public boolean isLoggedIn() { AccessToken accessToken = AccessToken.getCurrentAccessToken(); return accessToken != null; } 

我知道了!

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

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

这将在Current Access Tokes发生变化时被调用。 意思是,这只会帮助你,如果用户已经login。

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

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

那是为我做的! =]

使用AccessToken和AccessTokenTracker来检查login状态的困境是,当AccessToken准备就绪,并且追踪器的callback函数被调用,但configuration文件可能还没有准备好,因此我无法得到或显示Facebooker的名字。

我的解决scheme是检查当前configuration文件!= null,并使用其跟踪器同时拥有Facebooker的名字:

  ProfileTracker fbProfileTracker = new ProfileTracker() { @Override protected void onCurrentProfileChanged(Profile oldProfile, Profile currentProfile) { // User logged in or changed profile } }; 

检查login状态,然后获取用户名:

 Profile profile = Profile.getCurrentProfile(); if (profile != null) { Log.v(TAG, "Logged, user name=" + profile.getFirstName() + " " + profile.getLastName()); } 

你可以用同样的方式在菲利普的答案中提到,或者你可以用另外两种方法。 但是,似乎AccessTokenTracker是方便的方式,因为它可以帮助您跟踪访问令牌(与ProfileTracker类一起使用)

  1. 如果您使用自定义button进行login,请使用LoginManager进行回叫

例如

在你的布局xml中

  <Button android:id="@+id/my_facebook_button" android:background="@drawable/btnfacebook" android:onClick="facebookLogin"/> 

在你的活动

  //Custom Button Button myFacebookButton = (Button) findViewById(R.id.my_facebook_button); 

buttononclick监听器

 public void facebookLogin(View view) { LoginManager.getInstance().logInWithReadPermissions(this, Arrays.asList("public_profile", "user_friends")); } 

LoginManagercallback结束

  //Create callback manager to handle login response CallbackManager callbackManager = CallbackManager.Factory.create(); LoginManager.getInstance().registerCallback(callbackManager, new FacebookCallback<LoginResult>() { @Override public void onSuccess(LoginResult loginResult) { Log.i(TAG, "LoginManager FacebookCallback onSuccess"); if(loginResult.getAccessToken() != null) { Log.i(TAG, "Access Token:: " + loginResult.getAccessToken()); facebookSuccess(); } } @Override public void onCancel() { Log.i(TAG, "LoginManager FacebookCallback onCancel"); } @Override public void onError(FacebookException e) { Log.i(TAG, "LoginManager FacebookCallback onError"); } }); 
  1. 如果您使用SDK中提供的button(com.facebook.login.widget.LoginButton),请使用LoginButtoncallback函数(这在参考文档中有详细介绍 – https://developers.facebook.com/docs/facebook-login/android /v2.3 )

例如

在你的布局xml中

 <com.facebook.login.widget.LoginButton android:id="@+id/login_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal"/> 

在你的活动中

  //Facebook SDK provided LoginButton LoginButton loginButton = (LoginButton) findViewById(R.id.login_button); loginButton.setReadPermissions("user_friends"); //Callback registration loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() { @Override public void onSuccess(LoginResult loginResult) { // App code Log.i(TAG, "LoginButton FacebookCallback onSuccess"); if(loginResult.getAccessToken() != null){ Log.i(TAG, "Access Token:: "+loginResult.getAccessToken()); facebookSuccess(); } } @Override public void onCancel() { // App code Log.i(TAG, "LoginButton FacebookCallback onCancel"); } @Override public void onError(FacebookException exception) { // App code Log.i(TAG, "LoginButton FacebookCallback onError:: "+exception.getMessage()); Log.i(TAG,"Exception:: "+exception.getStackTrace()); } }); 

不要忘记调用callbackManager.onActivityResult(requestCode, resultCode, data); 在你的Activity onActivityResult()

其迟到了,但是现在在Facebook SDK 4.25.0版本中有一个方法:

 public void retrieveLoginStatus(Context context, LoginStatusCallback responseCallback) 

其中说:

检索用户的login状态。 如果用户在同一设备上loginFacebook for Android应用程序,并且该用户以前login过应用程序,则会返回该应用程序的访问令牌。 如果检索到访问令牌,则会显示一个通知,告诉用户他们已经login。

并可以使用像:

 LoginManager.getInstance().retrieveLoginStatus( this, new LoginStatusCallback() { @Override public void onCompleted( AccessToken accessToken ) { GraphRequest request = GraphRequest.newMeRequest( accessToken, new GraphRequest.GraphJSONObjectCallback() { @Override public void onCompleted( JSONObject object, GraphResponse response ) { Log.e( TAG, object.toString() ); Log.e( TAG, response.toString() ); try { userId = object.getString( "id" ); profilePicture = new URL( "https://graph.facebook.com/" + userId + "/picture?width=500&height=500" ); Log.d( "PROFILE_URL", "url: " + profilePicture.toString() ); if ( object.has( "first_name" ) ) { firstName = object.getString( "first_name" ); } if ( object.has( "last_name" ) ) { lastName = object.getString( "last_name" ); } if ( object.has( "email" ) ) { email = object.getString( "email" ); } if ( object.has( "birthday" ) ) { birthday = object.getString( "birthday" ); } if ( object.has( "gender" ) ) { gender = object.getString( "gender" ); } Intent main = new Intent( LoginActivity.this, MainActivity.class ); main.putExtra( "name", firstName ); main.putExtra( "surname", lastName ); main.putExtra( "imageUrl", profilePicture.toString() ); startActivity( main ); finish(); } catch ( JSONException e ) { e.printStackTrace(); } catch ( MalformedURLException e ) { e.printStackTrace(); } } } ); //Here we put the requested fields to be returned from the JSONObject Bundle parameters = new Bundle(); parameters.putString( "fields", "id, first_name, last_name, email, birthday, gender" ); request.setParameters( parameters ); request.executeAsync(); } @Override public void onFailure() { Toast.makeText( LoginActivity.this, "Could not log in.", Toast.LENGTH_SHORT ).show(); } @Override public void onError( Exception exception ) { Toast.makeText( LoginActivity.this, "Could not log in.", Toast.LENGTH_SHORT ).show(); } } );