Facebook和Google Plus集成在Android应用程序中

时间:2014-11-11 16:46:06

标签: android facebook facebook-graph-api android-activity google-plus

我试图在我的Android应用中集成facebook和google plus登录,并且它们单独运行良好。

当我尝试将它们同时整合时,它们似乎工作正常。

这是我的结构流程:

1.LoginActivity ---->用户点击google +或facebook登录按钮。

  1. 如果用户进行身份验证,则会将其移至我的MainActivity。
  2. 这是我的LoginActivity:

    public class GoogleLogin extends Activity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {
    private static final int RC_SIGN_IN = 0;
    
    private GoogleApiClient mGoogleApiClient;
    
    private boolean mIntentInProgress;
    
    private boolean mSignInClicked;
    
    private ConnectionResult mConnectionResult;
    
    public static GoogleLogin mContext;
    
    private UiLifecycleHelper uiHelper;
    
    Button fbutton;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mContext = this;
    
    uiHelper = new UiLifecycleHelper(this, statusCallback);
    uiHelper.onCreate(savedInstanceState);
    
    
    setContentView(R.layout.login);
    mGoogleApiClient = new GoogleApiClient.Builder(this)
    .addConnectionCallbacks(this)
    .addOnConnectionFailedListener(this)
    .addApi(Plus.API, PlusOptions.builder().build())
    .addScope(Plus.SCOPE_PLUS_LOGIN)
    .build();
    
    
    findViewById(R.id.sign_in_button).setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
    // TODO Auto-generated method stub
    if (v.getId() == R.id.sign_in_button
    && !mGoogleApiClient.isConnecting()) {
    mSignInClicked = true;
    resolveSignInError();
    }
    }
    });
    
    
    
    fbutton = (Button) findViewById(R.id.btnflogin);
    
    findViewById(R.id.btnflogin).setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View arg0) {
    
        // start Facebook Login
        Session.openActiveSession(GoogleLogin.this, true, new Session.StatusCallback() {
    
          // callback when session changes state
          @Override
          public void call(Session session, SessionState state, Exception exception) {
            if (session.isOpened()) {
    
              // make request to the /me API
              Request.newMeRequest(session, new Request.GraphUserCallback() {
    
                // callback after Graph API response with user object
                @Override
                public void onCompleted(GraphUser user, Response response) {
                  if (user != null) {
    
    
                      Intent i = new Intent(GoogleLogin.this,MainActivity.class);
    
    
                        //Add your data from getFactualResults method to bundle
                        i.putExtra("Facebook", "Logged in using Facebook Account");
                        i.putExtra("LastName", user.getLastName());
                        i.putExtra("FirstName", user.getFirstName());
                        //Add the bundle to the intent
                        i.putExtras(i);
                        startActivityForResult(i, 1);
                   }
                }
              }).executeAsync();
            }
          }
        });
    
    
    }
    });
    
    }
    
    
    private Session.StatusCallback statusCallback = new Session.StatusCallback() {
        @Override
        public void call(Session session, SessionState state,
                Exception exception) {
            if (state.isOpened()) {
    
                Log.d("FacebookSampleActivity", "Facebook session opened");
            } else if (state.isClosed()) {
    
                Log.d("FacebookSampleActivity", "Facebook session closed");
            }
        }
    };
    
    @Override
    public void onResume() {
        super.onResume();
        uiHelper.onResume();
    }
    
    @Override
    public void onPause() {
        super.onPause();
        uiHelper.onPause();
    }
    
    @Override
    public void onDestroy() {
        super.onDestroy();
        uiHelper.onDestroy();
    }
    
    
    @Override
    public void onSaveInstanceState(Bundle savedState) {
        super.onSaveInstanceState(savedState);
        uiHelper.onSaveInstanceState(savedState);
    }
    
    
    
    private void resolveSignInError() {
    if (mConnectionResult.hasResolution()) {
    try {
    mIntentInProgress = true;
    startIntentSenderForResult(mConnectionResult.getResolution().getIntentSender(),
    RC_SIGN_IN, null, 0, 0, 0);
    } catch (SendIntentException e) {
    // The intent was canceled before it was sent. Return to the default
    // state and attempt to connect to get an updated ConnectionResult.
    mIntentInProgress = false;
    mGoogleApiClient.connect();
    }
    }
    }
    
    protected void onActivityResult(int requestCode, int responseCode, Intent intent) {
        uiHelper.onActivityResult(requestCode, responseCode, intent);
    
    if (requestCode == RC_SIGN_IN) {
    if (responseCode != RESULT_OK) {
    mSignInClicked = false;
    }
    
    mIntentInProgress = false;
    
    if (!mGoogleApiClient.isConnecting()) {
    mGoogleApiClient.connect();
    }
    }
    
    }
    
    protected void onStart() {
    super.onStart();
    mGoogleApiClient.connect();
    }
    
    
    protected void onStop() {
    super.onStop();
    if (mGoogleApiClient.isConnected()) {
    mGoogleApiClient.disconnect();
    }
    }
    
    public void onConnectionFailed(ConnectionResult result) {
    if (!mIntentInProgress) {
    // Store the ConnectionResult so that we can use it later when the user clicks
    // 'sign-in'.
    mConnectionResult = result;
    
    if (mSignInClicked) {
    // The user has already clicked 'sign-in' so we attempt to resolve all
    // errors until the user is signed in, or they cancel.
    resolveSignInError();
    }
    }
    }
    
    public void onConnected(Bundle connectionHint) {
    // We've resolved any connection errors. mGoogleApiClient can be used to
    // access Google APIs on behalf of the user.
    mSignInClicked = false;
    Toast.makeText(this, "User is connected!", Toast.LENGTH_LONG).show();
    if (Plus.PeopleApi.getCurrentPerson(mGoogleApiClient) != null) {
    
    Intent i = new Intent(getApplicationContext(),MainActivity.class);
    Person currentPerson = Plus.PeopleApi.getCurrentPerson(mGoogleApiClient);
    
    //Create the bundle
    Bundle bundle = new Bundle();
    //Add your data from getFactualResults method to bundle
    bundle.putString("Google", "Logged in using Google Account");
    bundle.putString("GoogleUsername",  currentPerson.getDisplayName());
    //Add the bundle to the intent
    i.putExtras(bundle);
    startActivity(i);
    
    }
    
    }
    
    public void onConnectionSuspended(int cause) {
    
    mGoogleApiClient.connect();
    }
    
    public void gplusSignout(View v){
    //if (v.getId() == R.id.gPlusSignout) {
    if (mGoogleApiClient.isConnected()) {
    Plus.AccountApi.clearDefaultAccount(mGoogleApiClient);
    mGoogleApiClient.disconnect();
    //mGoogleApiClient.connect();
    Log.d("Debug","Disconnected");
    Toast.makeText(this, "User is disconnected!", Toast.LENGTH_LONG).show();
    }
    //}
    }
    
    
    }
    

    这些让我感到困惑:

    private void resolveSignInError() {
    if (mConnectionResult.hasResolution()) {
    try {
    mIntentInProgress = true;
    startIntentSenderForResult(mConnectionResult.getResolution().getIntentSender(),
    RC_SIGN_IN, null, 0, 0, 0);
    } catch (SendIntentException e) {
    // The intent was canceled before it was sent. Return to the default
    // state and attempt to connect to get an updated ConnectionResult.
    mIntentInProgress = false;
    mGoogleApiClient.connect();
    }
    }
    }
    
    protected void onActivityResult(int requestCode, int responseCode, Intent intent) {
    
    if (requestCode == RC_SIGN_IN) {
    if (responseCode != RESULT_OK) {
    mSignInClicked = false;
    }
    
    mIntentInProgress = false;
    
    if (!mGoogleApiClient.isConnecting()) {
    mGoogleApiClient.connect();
    }
    }
    
    }
    
    protected void onStart() {
    super.onStart();
    mGoogleApiClient.connect();
    }
    
    
    protected void onStop() {
    super.onStop();
    if (mGoogleApiClient.isConnected()) {
    mGoogleApiClient.disconnect();
    }
    }
    
    public void onConnectionFailed(ConnectionResult result) {
    if (!mIntentInProgress) {
    // Store the ConnectionResult so that we can use it later when the user clicks
    // 'sign-in'.
    mConnectionResult = result;
    
    if (mSignInClicked) {
    // The user has already clicked 'sign-in' so we attempt to resolve all
    // errors until the user is signed in, or they cancel.
    resolveSignInError();
    }
    }
    }
    

    我现在很震惊,我不确定我是否可以在ActivityResult中这样做:

    要启动Facebook并转到下一个活动,这可以正常工作:

    @Override
          public void onActivityResult(int requestCode, int resultCode, Intent data) {
              super.onActivityResult(requestCode, resultCode, data);
             Session.getActiveSession().onActivityResult(this, requestCode, resultCode, data);
    
        }
    

    但与此同时我有google + onActivity的结果。那么如何管理它们呢?

    任何人都可以说我如何实现这一目标,以使两者都有效。

0 个答案:

没有答案