1

我已经将 facebook 与 libgdx 集成,在我的主要课程中,我调用了 facebook 对话框和另一个屏幕,我希望在登录后,它会转到另一个屏幕,

if (mFacebook.isSessionValid()) {
        String name = SessionStore.getName(this);
        name        = (name.equals("")) ? "Unknown" : name;
        pk          = name;
        initialize(new GO(), false);
    }
    else{
        initialize(new GOloginscreen(), false);
        mFacebook.authorize(this, PERMISSIONS, -1, new FbLoginDialogListener());
    }

如果它有登录会话,它将进入主菜单屏幕,但如果没有,它将调用 fb 登录对话框。登录后我在这里设置初始化

public void onComplete(Bundle values) {
        SessionStore.save(mFacebook, AndroidStarter.this);
        getFbName();
        initialize(new GO(), false);
    }

但它不起作用,因为初始化必须在 oncreate 方法中?

有谁知道我该怎么做,或者有其他解决方案?

我一直坚持这一点,这里是我的完整代码

public class AndroidStarter extends AndroidApplication{
private Facebook mFacebook;
private ProgressDialog mProgress;
String PERMISSIONS[] = { "read_stream", "user_relationship_details",
        "user_religion_politics", "user_work_history",
        "user_relationships", "user_interests", "user_likes",
        "user_location", "user_hometown", "user_education_history",
        "user_activities", "offline_access" };
String APP_ID = "thismyfbid2133";
public static String pk;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    pk = "id";
    // Do the stuff that initialize() would do for you
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
                    WindowManager.LayoutParams.FLAG_FULLSCREEN);
    getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);

    mProgress       = new ProgressDialog(this);
    mFacebook       = new Facebook(APP_ID);       
    SessionStore.restore(mFacebook, this);
    if (mFacebook.isSessionValid()) {
        String name = SessionStore.getName(this);
        name        = (name.equals("")) ? "Unknown" : name;
        pk          = name;
        initialize(new GO(), false);
    }
    else{
        initialize(new GOloginscreen(), false);
        mFacebook.authorize(this, PERMISSIONS, -1, new FbLoginDialogListener());
    }
}


private final class FbLoginDialogListener implements DialogListener {
    public void onComplete(Bundle values) {
        SessionStore.save(mFacebook, AndroidStarter.this);
        getFbName();
    }

    public void onFacebookError(FacebookError error) {
        Toast.makeText(AndroidStarter.this, "Facebook connection failed", Toast.LENGTH_SHORT).show();
    }

    public void onError(DialogError error) {
        Toast.makeText(AndroidStarter.this, "Facebook connection failed", Toast.LENGTH_SHORT).show(); 
    }

    public void onCancel() {
    }
}

private void getFbName() {
    mProgress.setMessage("Finalizing ...");
    mProgress.show();

    new Thread() {
        @Override
        public void run() {
            String name = "";
            int what = 1;

            try {
                String me = mFacebook.request("me");

                JSONObject jsonObj = (JSONObject) new JSONTokener(me).nextValue();
                name = jsonObj.getString("name");
                what = 0;
            } catch (Exception ex) {
                ex.printStackTrace();
            }

            mFbHandler.sendMessage(mFbHandler.obtainMessage(what, name));
        }
    }.start();
}

private Handler mFbHandler = new Handler() {
    @Override
    public void handleMessage(Message msg) {
        mProgress.dismiss();

        if (msg.what == 0) {
            String username = (String) msg.obj;
            username = (username.equals("")) ? "No Name" : username;

            SessionStore.saveName(username, AndroidStarter.this);

            Toast.makeText(AndroidStarter.this, "Connected to Facebook as " + username, Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(AndroidStarter.this, "Connected to Facebook", Toast.LENGTH_SHORT).show();
        }
    }
};

}

4

0 回答 0