恢复Facebook 3 SDK会话

时间:2013-01-25 14:55:45

标签: android facebook

我正在使用Facebook登录构建一个Android应用程序,我坚持将会话保持到内部内存。这就是我实现它的方式:

private static Session openActiveSession(Activity activity, boolean allowLoginUI, StatusCallback callback, List<String> permissions) {
    OpenRequest openRequest = new OpenRequest(activity).setPermissions(permissions).setCallback(callback);
    Session session = getSession(activity);
    if (SessionState.CREATED_TOKEN_LOADED.equals(session.getState()) || allowLoginUI) {
        Session.setActiveSession(session);
        session.openForRead(openRequest);
        return session;
    }
    return null;
}

private static Session getSession(Activity activity) {
    return new Builder(activity).setTokenCachingStrategy(new SharedPreferencesTokenCachingStrategy(activity)).build();
}

问题是检查会话是否可以恢复的代码永远不会触发,因为isOpened()将始终返回false

if((facebookSession = getSession(this)) != null && facebookSession.isOpened()) {
        waitDialog = ProgressDialog.show(this, getResources().getString(R.string.pleaseWait), getResources().getString(R.string.loadingData));
        onPostLogin();
        return;
    }

我尝试用这两个函数解决这个问题:

private void saveFacebookSession() {
Bundle facebookCache = new Bundle();
        Parcel parceledCache = Parcel.obtain();
        Session.saveSession(facebookSession, facebookCache);
        parceledCache.writeBundle(facebookCache);
        try {
            FileOutputStream output = openFileOutput(FACEBOOK_SESSION_FILE, MODE_PRIVATE);
            byte[] marshalledParcel = parceledCache.marshall();
            Editor prefsEditor = preferences.edit();
            prefsEditor.putInt(RESTORE_BYTE_COUNT, marshalledParcel.length);
            prefsEditor.commit();
            output.write(marshalledParcel);
            output.flush();
            output.close();
        } catch (Exception e) {
            Log.e(getClass().getName(), "Could not save the facebook session to storage");
        }
}



private boolean restoreFacebookSession() {
        try {
            FileInputStream input = openFileInput(FACEBOOK_SESSION_FILE);
            int arraySize = preferences.getInt(RESTORE_BYTE_COUNT, -1);
            if(arraySize == -1) {
                Log.e(getClass().getName(), "Could not read the facebook restore size");
                return false;
            }
            byte[] sessionData = new byte[arraySize];
            input.read(sessionData);
            Parcel readParcel = Parcel.obtain();
            readParcel.unmarshall(sessionData, 0, arraySize);
            Bundle sessionBundle = readParcel.readBundle();
            facebookSession = Session.restoreSession(getApplicationContext(), null, (StatusCallback)this, sessionBundle);
            return true;
        } catch (Exception e) {
            Log.e(getClass().getName(), "Could not restore the session");
            return false;
        }
    }

第二种方法不起作用,因为在读取包时会抛出一个关于幻数的异常。

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

我遇到了同样的问题,问题是你第一次创建Session后没有调用Session.setActiveSession()所以我猜你就像我在onActivityResult方法上做的那样:

@Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        com.facebook.Session fbSession = com.facebook.Session.getActiveSession();
        if (fbSession != null) {
            fbSession.onActivityResult(this, requestCode, resultCode, data);
        }
    }

所以从来没有从facebook sdk读取回调getActiveSession总是返回null。