Google+ login button on Android does nothing when clicked

I am trying to implement a Google+ login in my android app. I followed the Google+ API tutorials. However, when I press the login button, nothing happens, no dialog or prompt appears for login. Logcat also shows no errors. I can't figure out why this doesn't work when I followed the Google+ API docs.

Here is my code:

package com.chromiumapps.fost;

import android.content.Intent;
import android.content.IntentSender.SendIntentException;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
import static android.view.View.*;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesClient.ConnectionCallbacks;
import com.google.android.gms.common.GooglePlayServicesClient.OnConnectionFailedListener;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.plus.Plus;

public class FostActivity extends FragmentActivity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, OnClickListener {

    /* Called when the activity is first created. */

    /*
     * public boolean Facebook = false; public boolean Twitter = false; public
     * boolean Googleplus = false; public boolean Blogger = false;
     */

    private MainFragment mainFragment;
    SharedPreferences pref;




    private static final int RC_SIGN_IN=0;
    private GoogleApiClient mGoogleApiClient;
    private boolean mIntentInProgress;
    private boolean mSignInClicked;

    private ConnectionResult mConnectionResult;




    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        pref = getPreferences(0);
        SharedPreferences.Editor edit = pref.edit();
        edit.putString("CONSUMER_KEY", CONSUMER_KEY);
        edit.putString("CONSUMER_SECRET", CONSUMER_SECRET);
        edit.commit();

        if (savedInstanceState == null) {
            // Add the fragment on initial activity setup
            mainFragment = new MainFragment();
            getSupportFragmentManager().beginTransaction()
                    .add(android.R.id.content, mainFragment).commit();
            //s = new ShareBarActivity();
            //getSupportFragmentManager().beginTransaction()
                    //.add(android.R.id.content, s).commit();


        } else {
            // Or set the fragment from restored state info
            mainFragment = (MainFragment) getSupportFragmentManager().findFragmentById(android.R.id.content);
            //linkedInFragment = (LinkedIn_Fragment) getSupportFragmentManager().findFragmentById(android.R.id.content);

        }
        setContentView(R.layout.main);


        findViewById(R.id.sign_in_button).setOnClickListener(this);
        findViewById(R.id.sign_out_button).setOnClickListener(this);
        mGoogleApiClient = new GoogleApiClient.Builder(this)
                            .addConnectionCallbacks(this)
                            .addOnConnectionFailedListener(this)
                            .addApi(Plus.API, new Plus.PlusOptions.Builder().build())
                            .addScope(Plus.SCOPE_PLUS_LOGIN)
                            .build();


    }

    protected void onStart(){
        super.onStart();
        mGoogleApiClient.connect();
    }

    protected void onStop(){
        super.onStop();
        if (mGoogleApiClient.isConnected()){
            mGoogleApiClient.disconnect();

        }

    }
    protected void onAcitivityResult(int requestCode, int ResponseCode, Intent intent){
        if (requestCode==RC_SIGN_IN){
            if (ResponseCode != RESULT_OK) {
                  mSignInClicked = false;
                }

                mIntentInProgress = false;

             if (!mGoogleApiClient.isConnecting()) {
                 mGoogleApiClient.connect();
              }
        }
    }



    @Override
    public void onConnected(Bundle arg0) {
        // TODO Auto-generated method stub
        mSignInClicked = false;

    }

    @Override
    public void onConnectionSuspended(int arg0) {
        // TODO Auto-generated method stub
        mGoogleApiClient.connect();
    }



    public void onConnectionFailed(ConnectionResult result) {
        // TODO Auto-generated method stub
        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();
            }
          }

    }

    protected 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();
                }
              } 

    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        if(v.getId()==R.id.sign_in_button && !mGoogleApiClient.isConnecting()){
            mSignInClicked=true;

            resolveSignInError();

        }

        if(v.getId()==R.id.sign_out_button){
            if(mGoogleApiClient.isConnected()){
                Plus.AccountApi.clearDefaultAccount(mGoogleApiClient);
                mGoogleApiClient.disconnect();
                mGoogleApiClient.connect();
            }
        }


    }
}

      

+3


source to share


1 answer


Try this in mGoogleApiClient



   mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addApiIfAvailable(Drive.API)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this).addApi(Plus.API, Plus.PlusOptions.builder().build())
            .addScope(Plus.SCOPE_PLUS_LOGIN).build();

      

0


source







All Articles