在FB Android SDK中DOC使得該參考:的Android + +的Facebook「響應」參數
「。然而,注意,這並不考慮其中用戶可能已經撤銷訪問的情況您的應用程序或用戶更改了密碼,您需要始終注意無效的access_token並重定向用戶重新授權您的應用程序。對於無效的訪問令牌,在onComplete的「響應」參數中返回以下錯誤( )方法:「
文檔提供了一個共享首選項解決方案,用於每次用戶啓動您的應用程序時隱藏」OKAY「頁面。但是,如果用戶更改其密碼或從fb應用程序本身註銷,則我的應用程序(因爲設置了共享首選項)會忽略登錄對話框,因爲不會調用facebook.authorize()。鑑於文檔中的實現,當onComplete()再也不會被調用時,如何監聽access_token錯誤。
package com.greatapp;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import com.facebook.android.*;
import com.facebook.android.Facebook.*;
public class MyGreatActivity extends Activity {
Facebook facebook = new Facebook("YOUR_APP_ID");
String FILENAME = "AndroidSSO_data";
private SharedPreferences mPrefs;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
/*
* Get existing access_token if any
*/
mPrefs = getPreferences(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);
}
if(expires != 0) {
facebook.setAccessExpires(expires);
}
/*
* Only call authorize if the access_token has expired.
*/
if(!facebook.isSessionValid()) {
facebook.authorize(this, new String[] {}, new DialogListener() {
@Override
public void onComplete(Bundle values) {
SharedPreferences.Editor editor = mPrefs.edit();
editor.putString("access_token", facebook.getAccessToken());
editor.putLong("access_expires", facebook.getAccessExpires());
editor.commit();
}
@Override
public void onFacebookError(FacebookError error) {}
@Override
public void onError(DialogError e) {}
@Override
public void onCancel() {}
});
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
facebook.authorizeCallback(requestCode, resultCode, data);
}
}