Android:跨活动传递Facebook会话

时间:2012-05-11 21:36:46

标签: android facebook

我希望通过各种活动传递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活动?有没有这样的例子?

3 个答案:

答案 0 :(得分: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();
        }
    }
    

答案 1 :(得分:2)

对于FB SDK 3.5,在我的FB登录活动中,我通过intent extras传递活动会话对象,因为Session类实现了serializable:

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"));
}

答案 2 :(得分:1)

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