3

我正在寻找跨活动传递 Facebook 会话。我从 Facebook 的 SDK 中看到了这个例子,有人提到“简单”的例子有办法做到这一点: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活动?有没有实施的例子?

4

3 回答 3

4

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

相反,您有两个选择:

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

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

    // 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();
        }
    }
    
于 2012-05-12T04:12:33.570 回答
2

对于 FB SDK 3.5,在我的 FB 登录活动中,我通过 Intent Extras 传递活动会话对象,因为 Session 类实现了可序列化:

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"));
}
于 2013-09-04T17:05:47.450 回答
1

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

于 2012-05-11T22:08:42.287 回答