这是我的代码:
mPrefs = getSharedPreferences("facebook_session",MODE_PRIVATE);
String access_token = mPrefs.getString("access_token", null);
long expires = mPrefs.getLong("access_expires", 0);
if(access_token != null) {
facebook.setAccessToken(access_token);
Log.i("access_token","facebook.setAccessToken(access_token), and access_token=="+access_token);
}
if(expires != 0) {
Log.i("expires","facebook.setAccessToken(access_token), and expires=="+expires);
facebook.setAccessExpires(expires);
}
/*
* Only call authorize if the access_token has expired.
*/
if(!facebook.isSessionValid()) {
Log.v("in !facebook.isSessionValid()", "no pref");
facebook.authorize( this, new String[] { "email", "publish_checkins" }, new DialogListener(){
@Override
public void onComplete(Bundle values) {
Log.v("facebook.authorize", "no pref");
Log.v("postToWall","mFacebook.getAccessToken(): "+facebook.getAccessToken());
Log.v("postToWall","mFacebook.getAccessExpires(): "+facebook.getAccessExpires());
SharedPreferences.Editor editor = mPrefs.edit();
editor.putString("access_token", facebook.getAccessToken());
editor.putLong("access_expires", facebook.getAccessExpires());
editor.commit();
}
@Override
public void onFacebookError(FacebookError e) {
Log.v("facebook.authorize","mFacebook.onFacebookError(): "+e);
}
@Override
public void onError(DialogError e) {
Log.v("facebook.authorize","mFacebook.onFacebookError(): "+e);
Log.v("facebook.authorize","mFacebook.onError(): "+facebook.getAccessToken());
}
@Override
public void onCancel() {
Toast.makeText(getApplicationContext(),
"You must be registered and signed in to perform that action",
Toast.LENGTH_LONG).show();
finish();
// onCancel, leaving the app
}
});
}//end of pref check
在注销功能中,我尝试从 facebook 注销并清除 sharedPreferences:public void Logout(View view){
String access_token = mPrefs.getString("access_token", null);
Log.i("access_token","mPrefs.getString(access_token, null), and access_token=="+access_token);
SharedPreferences.Editor editor = mPrefs.edit();
editor.remove("access_token");
editor.remove("access_expires");
editor.commit();
Log.i("access_token","mPrefs.getString(access_token, null), and access_token=="+access_token);
mAsyncRunner = new AsyncFacebookRunner(facebook); // initialize AsyncRunner, make sure it's not null
mAsyncRunner.logout(Login.this, new RequestListener() {
@Override
public void onComplete(String response, Object state) {
finish();
}
@Override
public void onIOException(IOException e, Object state) {}
@Override
public void onFileNotFoundException(FileNotFoundException e,
Object state) {}
@Override
public void onMalformedURLException(MalformedURLException e,
Object state) {}
@Override
public void onFacebookError(FacebookError e, Object state) {}
});
}
所以,问题来了:无论我多么努力,sharedPreferences 中存储的值都没有改变。我试过 clear()、remove()、commit()、apply() 等.....请帮忙!