我正在构建一个我正在使用Facebook登录的Android应用程序,在Facebook登录后,我获得了以私有模式存储在共享首选项中的access_token和session。
我在活动中使用下面的Facebook登录代码,我希望用户从另一个片段中从Facebook注销。如何对片段进行编码,以便我当前登录的帐户从Facebook注销?
这是我的Facebook登录代码:
/**
* Function to login into facebook
*/
@SuppressWarnings("deprecation")
public void loginToFacebook() {
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);
// call getprofileinformation function to get user details
getProfileInformation();
Log.d("FB Sessions", "" + facebook.isSessionValid());
}
if (expires != 0) {
facebook.setAccessExpires(expires);
}
if (!facebook.isSessionValid()) {
facebook.authorize(this,
new String[]{"email", "publish_stream"},
new Facebook.DialogListener() {
@Override
public void onCancel() {
// Function to handle cancel event
}
@Override
public void onComplete(Bundle values) {
// Function to handle complete event
// Edit Preferences and update facebook acess_token
SharedPreferences.Editor editor = mPrefs.edit();
editor.putString("access_token",
facebook.getAccessToken());
editor.putLong("access_expires",
facebook.getAccessExpires());
editor.commit();
// call getprofileinformation function to get user
// details
getProfileInformation();
}
@Override
public void onError(DialogError error) {
// Function to handle error
}
@Override
public void onFacebookError(FacebookError fberror) {
// Function to handle Facebook errors
}
});
}
}
答案 0 :(得分:1)
对于 4.x.x 版本 -
LoginManager.getInstance().logOut();
答案 1 :(得分:0)
试试这个:
private static void LogoutFB(Context context) {
Session session = Session.getActiveSession();
if (session != null) {
if (!session.isClosed()) {
session.closeAndClearTokenInformation();
//clear your preferences if saved
}
} else {
session = new Session(context);
Session.setActiveSession(session);
session.closeAndClearTokenInformation();
//clear your preferences if saved
}
}
答案 2 :(得分:0)
调用此closeAndClearTokenInformation方法从facebook注销。 此方法关闭本地内存中的Session对象,并清除与Session相关的任何持久性令牌缓存。