2012-09-03 89 views
1

這裏是我的代碼:Android Facebook sdk,sharedPreferences永遠不會改變?

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還有: 公共無效註銷(查看視圖){

 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中的值都沒有改變。我嘗試清除(),刪除(),提交(),應用()等..... 請幫助!

+0

謝謝你提醒我 – crystalWing

+0

請人幫幫我!! – crystalWing

回答

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); 
相關問題