我有SettingsActivity
几个选项,包括查找 Facebook 朋友和注销。因此,当用户选择查找她的 Facebook 朋友时,她将被发送到另一个活动,我让她使用她的 Facebook 帐户登录并保存她的访问令牌。然后当她选择注销时,她的访问令牌将被清除。问题是,我的logout
方法与我创建会话的方法没有写在同一个活动中,所以当我尝试这个时:
Session session = Session.getActiveSession();
if (!session.isClosed()) {
session.closeAndClearTokenInformation();
}
Logcat 点NullPointerException
。然后我尝试了:
Session.setActiveSession(null);
这也不起作用(仍然显示之前登录的用户的朋友,而不是要求新用户登录)。
那么我应该怎么做才能清除获得的令牌呢?先感谢您。
-编辑-
我认为我用来打开会话的活动有问题。我关注了 Facebook 的GraphAPISample,这就是我所做的:
public class FacebookFriendsActivity extends FragmentActivity{
private BroadcastReceiver broadcast;
public static Session session;
private boolean pendingRequest;
static final String PENDING_REQUEST_BUNDLE_KEY = "PendingRequest";
private DatabaseHandler db;
private FindFacebookFriends task;
private ProgressBar progress;
private TextView text;
private Button retryBtn;
private ListView userLayout;
private ArrayList<User> userList;
private FollowAdapter adapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.facebook_friends);
// ... set views
session = createSession();
Settings.addLoggingBehavior(LoggingBehavior.INCLUDE_ACCESS_TOKENS);
showFriends();
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (session.onActivityResult(this, requestCode, resultCode, data) &&
pendingRequest &&
session.getState().isOpened()) {
showFriends();
}
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
pendingRequest = savedInstanceState.getBoolean(PENDING_REQUEST_BUNDLE_KEY, pendingRequest);
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putBoolean(PENDING_REQUEST_BUNDLE_KEY, pendingRequest);
}
private void showFriends(){
task = new FindFacebookFriends();
task.execute(session.getAccessToken());
}
private Session createSession() {
Session activeSession = Session.getActiveSession();
if (activeSession == null || activeSession.getState().isClosed()) {
activeSession = new Session.Builder(this).setApplicationId(getString(R.string.fb_app_id)).build();
Session.setActiveSession(activeSession);
new SaveFacebookId().execute(activeSession.getAccessToken());
}
return activeSession;
}
}
当我尝试输入一个Toast
来createSession()
检查 created 的状态时activeSession
,它总是显示会话已关闭。这很奇怪,因为该showFriends()
方法仍然被触发,即使我再次删除并安装该应用程序后,它仍然显示相同的朋友列表。我在这里真的很困惑。