我已经用4.0 sdk实现了facebook登录,在成功登录后,我获得了一个加号令牌并继续主要活动,一切都很完美。 但是当我关闭并重新打开应用程序时,accesstoken为null。我不知道如何解决它
这是我的代码:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FacebookSdk.sdkInitialize(getApplicationContext());
if (BuildConfig.DEBUG) {
FacebookSdk.setIsDebugEnabled(true);
FacebookSdk.addLoggingBehavior(LoggingBehavior.INCLUDE_ACCESS_TOKENS);
}
setContentView(R.layout.activity_login_fb);
prefs = getSharedPreferences("config", Context.MODE_PRIVATE);
editor = prefs.edit();
token_session = "";
callbackManager = CallbackManager.Factory.create();
LoginManager.getInstance().logOut();
accessTokenTracker = new AccessTokenTracker() {
@Override
protected void onCurrentAccessTokenChanged(AccessToken oldAccessToken, AccessToken newAccessToken) {
AccessToken.setCurrentAccessToken(newAccessToken);
updateWithToken(newAccessToken);
}
};
accessToken = AccessToken.getCurrentAccessToken();
updateWithToken(AccessToken.getCurrentAccessToken());
accessTokenTracker.startTracking();
startApp();
}
private void startApp(){
loginButton = (LoginButton)findViewById(R.id.login_button);
loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
AccessToken.setCurrentAccessToken(loginResult.getAccessToken());
sendLoginToBackend();
//Log.i("Facebook Login", loginResult.getAccessToken().getToken());
}
@Override
public void onCancel() {
//Log.i("Facebook Login", "Login Cancel");
}
@Override
public void onError(FacebookException e) {
Log.e("Facebook Login", "Login Error");
}
});
}
private void updateWithToken(AccessToken currentAccessToken) {
if (currentAccessToken == null){
Log.i("Facebook Access Token ","null");
}else{
Log.i("Facebook Access Token ","not null");
}
if (currentAccessToken != null) {
if (prefs.getBoolean("datos_completos",false)){
new Handler().postDelayed(new Runnable() {
// THIS WILL BE REMOVED
@Override
public void run() {
//Intent i = new Intent(self, MainActivity.class);
//startActivity(i);
//finish();
}
}, 500);
}
}
else{
Toast.makeText(this,"NO FB TOKEN",Toast.LENGTH_LONG).show();
}
}
答案 0 :(得分:0)
您每次都在Activity的onCreate方法中调用logout。
LoginManager.getInstance().logOut();
答案 1 :(得分:0)
只需删除此行
即可LoginManager.getInstance().logOut();
在onCreate方法上调用logout方法意味着每次应用程序启动时都会从facebook注销。