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。

代わりに、次の 2 つのオプションがあります。

  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 に答える
1

この例には、実装全体がほとんど含まれています。SharedPreferencesセッションを保存するために使用するだけです。PhotoActivity でそれが必要な場合は、(同じパターンに従った場合はSharedPreferencesおそらく静的メソッドを介して) もう一度調べて、以前に保存した Facebook セッションを取得します。SessionStore

于 2012-05-11T22:08:42.287 に答える