1

这是我的代码:

    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() 等.....请帮忙!

4

1 回答 1

0
  Log.i("access_token","mPrefs.getString(access_token, null), and access_token=="+access_token);

如果您使用此Log语句来验证存储的值是否SharedPreferences被清除,则说明您没有正确执行。

access_token的值在您删除它之前被分配了一个值,因此当您引用该变量时,它仍将保留该值。要正确测试该值是否已从 中删除SharedPreferences,您需要在删除该值后再次尝试访问该值,方法是在命令后执行以下操作editor.commit()

String access_token_2 = mPrefs.getString("access_token", null);
Log.i("access_token", "access_token from SharedPreferences is now " + access_token_2);
于 2012-09-04T22:04:30.817 回答