Android:跨活动传递Facebook会话



我希望在活动中传递Facebook会话。我看到了FacebookSDK的例子,有人提到"简单"的例子有一种方法可以做到这一点:https://github.com/facebook/facebook-android-sdk/blob/master/examples/simple/src/com/facebook/android/SessionStore.java

但这是如何工作的呢?在我的MainActivity中,我有这个:

mPrefs = getPreferences(MODE_PRIVATE);
String accessToken = mPrefs.getString("access_token", null);
long expires = mPrefs.getLong("access_expires", 0);
if (accessToken != null) {
    //We have a valid session! Yay!
    facebook.setAccessToken(accessToken);
}
if (expires != 0) {
    //Since we're not expired, we can set the expiration time.
    facebook.setAccessExpires(expires);
}
//Are we good to go? If not, call the authentication menu.
if (!facebook.isSessionValid()) {
    facebook.authorize(this, new String[] { "email", "publish_stream" }, new DialogListener() {
        @Override
        public void onComplete(Bundle values) {
        }
        @Override
        public void onFacebookError(FacebookError error) {
        }
        @Override
        public void onError(DialogError e) {
        }
        @Override
        public void onCancel() {
        }
    });
}

但是,我如何将它传递给我的PhotoActivity活动呢?有没有正在实施的例子?

使用 SharedPreferences 跨活动传递数据并不是一个好主意。共享首选项用于在应用程序重新启动或设备重新启动时将一些数据存储到内存中。

相反,您有两种选择:

  1. 声明一个静态变量来保存Facebook会话,这是最简单的方法,但我不建议使用静态字段,因为没有其他方法。

  2. 创建一个实现可分包
  3. 的类,并在那里设置你的 facebook 对象,查看可分包实现,如下所示:

    // simple class that just has one member property as an example
    public class MyParcelable implements Parcelable {
        private int mData;
        /* everything below here is for implementing Parcelable */
        // 99.9% of the time you can just ignore this
        public int describeContents() {
            return 0;
        }
        // write your object's data to the passed-in Parcel
        public void writeToParcel(Parcel out, int flags) {
            out.writeInt(mData);
        }
        // this is used to regenerate your object. All Parcelables must have a CREATOR that implements these two methods
        public static final Parcelable.Creator<MyParcelable> CREATOR = new Parcelable.Creator<MyParcelable>() {
            public MyParcelable createFromParcel(Parcel in) {
                return new MyParcelable(in);
            }
            public MyParcelable[] newArray(int size) {
                return new MyParcelable[size];
            }
        };
        // example constructor that takes a Parcel and gives you an object populated with it's values
        private MyParcelable(Parcel in) {
            mData = in.readInt();
        }
    }
    

对于FB SDK 3.5,在我的FB登录活动中,我通过意图附加内容传递活动会话对象,因为会话类实现了可序列化:

private void onSessionStateChange(Session session, SessionState state, Exception exception) {
    if (exception instanceof FacebookOperationCanceledException || exception instanceof FacebookAuthorizationException) {
        new AlertDialog.Builder(this).setTitle(R.string.cancelled).setMessage(R.string.permission_not_granted).setPositiveButton(R.string.ok, null).show();
    } else {
        Session session = Session.getActiveSession();
        if ((session != null && session.isOpened())) {
            // Kill login activity and go back to main
            finish();
            Intent intent = new Intent(getApplicationContext(), MainActivity.class);
            intent.putExtra("fb_session", session);
            startActivity(intent);
        }
    }
}

从我的 MainActivity onCreate(),我检查额外的意图并启动会话:

Bundle extras = getIntent().getExtras();
if (extras != null) {           
    Session.setActiveSession((Session) extras.getSerializable("fb_session"));
}

该示例几乎具有整个实现。您只需使用SharedPreferences来存储会话。当您在PhotoActivity中需要它时,只需再次查看SharedPreferences(如果您遵循相同的模式,则可能通过SessionStore静态方法)以获取您之前存储的Facebook会话。

最新更新