Facebook for Android Integration不能始终如一地运作

时间:2012-08-25 05:48:00

标签: java android facebook race-condition facebook-android-sdk

我正在尝试将Facebook登录与我的应用集成。这很简单:用户单击我的登录屏幕上的按钮,我从Facebook获取他们的电子邮件和其他信息,然后我将该信息提交到我的网站并使其成为一个帐户(我不使用密码,所以这有效)。 / p>

但是,登录按钮只能起到1/3的作用。其他时候它启动Facebook活动,我在Facebook屏幕上看到一个加载微调器,然后应用程序退出到我的主屏幕。没有强制关闭通知,没有logcat错误,只是没有。

以下是我的登录活动代码(与Facebook相关的部分):

package com.taptag.beta;

/** IMPORTS REMOVED FOR BREVITY **/

public class FacebookLogInActivity extends NetworkActivity {
    public static final String APP_ID = "OMITTED FOR PRIVACY";
    public static final String LOG_OUT = "Log Out";

    Facebook facebook = new Facebook(APP_ID);

    private AsyncFacebookRunner mAsyncRunner;
    private Handler mHandler;
    private SharedPreferences mPrefs;
    private Button mLoginButton;
    private static final String[] PERMISSIONS = { "email" };

    private EditText firstName;
    private EditText lastName;
    private EditText email;
    private Button signupButton;

    private TextView errorView;
    private ProgressBar facebookSpinner;
    private ProgressBar signupSpinner;

    //Taken from mkyong
    private static final String EMAIL_PATTERN = "^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
    private Pattern emailPattern;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.facebook_main);

        mPrefs = getSharedPreferences("TapTag", MODE_PRIVATE);
        Integer userId = mPrefs.getInt("user_id", -1);

        mLoginButton = (Button) findViewById(R.id.loginButton);

        // Facebook properties
        mAsyncRunner = new AsyncFacebookRunner(facebook);
        mHandler = new Handler();
        // Get existing saved session information
        String access_token = mPrefs.getString("access_token", null);
        long expires = mPrefs.getLong("access_expires", 0);

        if (access_token != null) {
            facebook.setAccessToken(access_token);
        }
        if (expires != 0) {
            facebook.setAccessExpires(expires);
        }

        errorView = (TextView) findViewById(R.id.signupError);
        facebookSpinner = (ProgressBar) findViewById(R.id.facebookSpinner);
        signupSpinner = (ProgressBar) findViewById(R.id.signupSpinner);

        hideSpinners();
        errorView.setVisibility(View.GONE);

        emailPattern = Pattern.compile(EMAIL_PATTERN);
        firstName = (EditText) findViewById(R.id.signupFirstName);
        lastName = (EditText) findViewById(R.id.signupLastName);
        email = (EditText) findViewById(R.id.signupEmail);
        signupButton = (Button) findViewById(R.id.signupButton);

        signupButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                boolean isValid = validateInputs();
                if (isValid) {
                    SignupTask signupTask = new SignupTask();
                    signupTask.execute(null, null);
                }
            }
        });

        mLoginButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!facebook.isSessionValid()) {
                    logIn();
                } else {
                    logOut();
                }
            }
        });
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        facebook.authorizeCallback(requestCode, resultCode, data);
    }

    @Override
    public void onResume() {
        super.onResume();
        // Extend the session information if it is needed
        //if (facebook != null && !facebook.isSessionValid()) {
            facebook.extendAccessTokenIfNeeded(this, null);
        //}
        if (LOG_OUT.equals(getIntent().getAction())) {
            logOut();
        } else  {
            Integer userId = mPrefs.getInt("user_id", -1);
            if (userId > 0) {
                continueToHomeScreen();
            }
        }
    }

    @Override
    public void onBackPressed() {
        //Do nothing, don't want people getting back into the app
    }

    /**
     * Validate the signup form.  True if valid, false otherwise.
     * @return
     */
    public boolean validateInputs() {
        /** NOT RELEVANT **/
    }

    private FacebookUserInfo userFromForm() {
        /** NOT RELEVANT **/
    }

    private boolean validateFirstName() {
        /** NOT RELEVANT **/
    }

    private boolean validateLastName() {
        /** NOT RELEVANT **/
    }

    private boolean validateEmail() {
        /** NOT RELEVANT **/
    }

    public void hideSpinners() {
        facebookSpinner.setVisibility(View.GONE);
        signupSpinner.setVisibility(View.GONE);
    }

    public void continueToHomeScreen() {
        Intent toHomeScreen = new Intent(FacebookLogInActivity.this, HomeScreenActivity.class);
        FacebookLogInActivity.this.startActivity(toHomeScreen);
    }

    public void logIn() {
        facebookSpinner.setVisibility(View.VISIBLE);
        mLoginButton.setVisibility(View.GONE);
        facebook.authorize(FacebookLogInActivity.this, PERMISSIONS, new DialogListener() {
            @Override
            public void onComplete(Bundle values) {
                SharedPreferences.Editor editor = mPrefs.edit();
                editor.putString("access_token", facebook.getAccessToken());
                editor.putLong("access_expires", facebook.getAccessExpires());
                editor.commit();
                requestUserData();
            }

            @Override
            public void onFacebookError(FacebookError e) {
                mLoginButton.setVisibility(View.VISIBLE);
                hideSpinners();

            }
            @Override
            public void onError(DialogError e) {
                mLoginButton.setVisibility(View.VISIBLE);
                hideSpinners();
            }
            @Override
            public void onCancel() {
                mLoginButton.setVisibility(View.VISIBLE);
                hideSpinners();
            }
        });
    }

    public void logOut() {
        /** NOT RELEVANT **/
    }

    public void requestUserData() {
        Bundle params = new Bundle();
        params.putString("field name", "name");
        params.putString("field email", "email");

        mAsyncRunner.request("me", params, new BaseRequestListener() {
            @Override
            public void onComplete(final String response, final Object state) {
                FacebookUserInfo facebookUserInfo = TapTagAPI.userInfoFromFacebook(response);
                UserFetchResponse userFetchResponse = TapTagAPI.fetchUser(facebookUserInfo);
                if (!userFetchResponse.hasError()) {
                    commitUserInfo(userFetchResponse);
                    continueToHomeScreen();
                } else {
                    mHandler.post(new Runnable() {
                        @Override
                        public void run() {
                            hideSpinners();
                            mLoginButton.setVisibility(View.VISIBLE);
                        }
                    });
                }
            }
        });
    }

    public void commitUserInfo(UserFetchResponse userFetchResponse) {
        SharedPreferences.Editor editor = mPrefs.edit();
        editor.putInt("user_id", userFetchResponse.getId());
        editor.putString("user_name", userFetchResponse.getFirst() + " " + userFetchResponse.getLast());
        editor.commit();
    }

    public class SignupTask extends AsyncTask<Void, Void, UserFetchResponse> {
        @Override
        protected void onPreExecute() {
            signupSpinner.setVisibility(View.VISIBLE);
            signupButton.setVisibility(View.GONE);
        }

        @Override
        protected UserFetchResponse doInBackground(Void... arg0) {
            FacebookUserInfo user = userFromForm();
            UserFetchResponse response = TapTagAPI.fetchUser(user);
            return response;
        }

        @Override
        protected void onPostExecute(UserFetchResponse response) {
            if (response.success()) {
                commitUserInfo(response);
                continueToHomeScreen();
            } else {
                errorView.setVisibility(View.VISIBLE);
            }
        }
    }

}

1 个答案:

答案 0 :(得分:2)

写下登录功能而不是登录功能,它将解决您的问题。

public void logIn() {
    facebookSpinner.setVisibility(View.VISIBLE);
    mLoginButton.setVisibility(View.GONE);
    facebook.authorize(FacebookLogInActivity.this, PERMISSIONS, Facebook.FORCE_DIALOG_AUTH, new DialogListener() {
        @Override
        public void onComplete(Bundle values) {
            SharedPreferences.Editor editor = mPrefs.edit();
            editor.putString("access_token", facebook.getAccessToken());
            editor.putLong("access_expires", facebook.getAccessExpires());
            editor.commit();
            requestUserData();
        }

        @Override
        public void onFacebookError(FacebookError e) {
            mLoginButton.setVisibility(View.VISIBLE);
            hideSpinners();
        }

        @Override
        public void onError(DialogError e) {
            mLoginButton.setVisibility(View.VISIBLE);
            hideSpinners();
        }

        @Override
        public void onCancel() {
            mLoginButton.setVisibility(View.VISIBLE);
            hideSpinners();
        }
    });
}