如何将会话保存到手机?

时间:2013-01-09 09:43:05

标签: android facebook session sharedpreferences

目前,我的会话始终为null,假设bundle不会为null。

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_splash_scene);
    this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

    RelativeLayout rLayout = (RelativeLayout) findViewById(R.id.splashbg);
    rLayout.setBackgroundResource(R.drawable.default_bg);

    Settings.addLoggingBehavior(LoggingBehavior.INCLUDE_ACCESS_TOKENS);

    Session session = Session.getActiveSession();

    if (session == null) {
        if (session == null) {
            session = new Session(this);
        }
        Session.setActiveSession(session);
    }

    session = Session.getActiveSession();
    if (session.getState().equals(SessionState.CREATED_TOKEN_LOADED)) {
        session.openForRead(new Session.OpenRequest(SplashScene.this)
                .setCallback(statusCallback));
    } else {
        updateView();
    }
}

@Override
public void onBackPressed() {
    // disable back button by overriding it
}

@Override
public void onStart() {
    super.onStart();
    Session.getActiveSession().addCallback(statusCallback);
}

@Override
public void onStop() {
    super.onStop();
    Session.getActiveSession().removeCallback(statusCallback);
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    Session.getActiveSession().onActivityResult(this, requestCode,
            resultCode, data);
}

@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    Session session = Session.getActiveSession();
    Session.saveSession(session, outState);
}

private class SessionStatusCallback implements Session.StatusCallback {
    @Override
    public void call(Session session, SessionState state,
            Exception exception) {
        if (session.isOpened()) {
            GlobalFunction.token = session.getAccessToken();
            IsExistMember checkmember = new IsExistMember();
            checkmember.execute();
        }
    }
}

private void updateView() {
    Session session = Session.getActiveSession();
    if (!session.isOpened() && !session.isClosed()
            && session.getState() != SessionState.OPENING) {
        OpenRequest open = new OpenRequest(this)
                .setCallback(statusCallback);
        List<String> permission = new ArrayList<String>();
        permission.add("email");
        open.setPermissions(permission);
        session.openForRead(open.setCallback(statusCallback));
    } else {
        Session.openActiveSession(this, true, statusCallback);
        // Intent intent = new Intent(SplashScene.this, WelcomeScene.class);
        // startActivity(intent);
        // finish();
    }

}

如何将会话保持在手机中,下次运行此应用时无需创建新会话。

我正在检查此Activity中的会话。如果存在,则继续执行其他活动以使用Facebook登录并将捆绑包保存到手机中。

根据教程,它被教导用Activity做所有事情,但我想在不同的Activity中单独做。

有类似sharedpreference的内容,我认为它与iOS appdelegate相同吗?

2 个答案:

答案 0 :(得分:0)

使用onRestoreInstanceStateonSaveInstanceState回调来存储您的值(状态变量)并在下次启动时恢复它们。

要好好了解这些如何工作,看看android活动的生命周期。 http://developer.android.com/training/basics/activity-lifecycle/recreating.html

答案 1 :(得分:0)

您始终需要创建新会话。但是,如果用户之前已经授权您的应用程序,则sessionState将为CREATED_TOKEN_LOADED,当您调用openForRead时,没有用户交互(它只是以保存的凭据静默打开)。

为什么你不想创建一个新的Session实例?