0

我有一项主要活动和一项服务,我每 x 小时更新一次后台从 facebook 获取的一些数据。

这些天来,我终于迁移到新的 SDK 3.0.1(从 2.x 开始),并且在将 facebook 会话传递给服务时遇到了问题:

如果在主要活动仍在运行时服务运行一切正常,否则我的会话为空,因此它在 if (session.isOpened()) { ...} 处崩溃,因为 session==null。

这是我的代码:

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    Log.i("ServiceUpdate", "Received start id " + startId + ": " + intent);

    session = Session.getActiveSession();

    startservice();
    return START_STICKY;
}


private void startservice() {

    timer.scheduleAtFixedRate(new TimerTask() {
        public void run() {         
            if (session.isOpened()) {
                if (isOnline()) {
                    Log.i("ServiceUpdate", "start timer");

                    update();

                    Log.i("ServiceUpdate", "end timer");
                }
             }

    }, 600000, 6 * 3600000);

}

我是否必须像以前使用 SDK 2.x 一样使用保存的 access_token 和 expire_time 来初始化会话?

谢谢!

4

1 回答 1

2

Session 的默认行为是将令牌信息保存在 Android 的 SharedPreferences 中,因此您应该能够在您的服务中调用 Session.openActiveSessionFromCache。如果返回 null,则表示您的用户尚未连接 Facebook。

于 2013-04-09T20:41:11.310 回答