0
我已經實現了facebook登錄4.0 sdk,成功登錄後,我得到一個accees令牌,並繼續主要活動,一切工作完美。 但是,當我關閉並重新打開應用程序,accesstoken爲空。我不知道如何解決它android facebook sdk總是要求重新登錄
這裏是我的代碼:
@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();
}
}
哇!我怎麼可能看不到這條線!! ??我想我對於沒有閱讀所有代碼的截止日期感到困惑!我很抱歉!謝謝! .....我不配稱爲程序員了。 – Emitate