Hiren Patel
Hiren Patel

Reputation: 52790

Android: Google+ integration. Calling connect() while still connected, missing disconnect()

I am working on app which is having Google+ integration in android app, I have tried to login with Google+ account, now PlusClient can not connect with account, what I did so far.

PlusClient mPlusClient;
mPlusClient.connect();

When I have checked that mPlusClient is connected or not, I got below resilt.

Log.i("PlusClient", ""+mPlusClient.isConnected());

Output is **False**.

Any help would be appreciated.

Upvotes: 1

Views: 4121

Answers (2)

Ketan Mehta
Ketan Mehta

Reputation: 272

Hope its helpful to you..this blog describe Google+ integration nicely

http://ankitthakkar90.blogspot.in/

Upvotes: 1

Luis Nuñez
Luis Nuñez

Reputation: 80

Read this before start: https://developers.google.com/+/mobile/android/getting-started And then this: https://developers.google.com/+/mobile/android/sign-in

You need to initialize:

  • Initialize the PlusClient object in your Activity.onCreate handler.

  • Invoke PlusClient.connect during Activity.onStart() .

  • Invoke PlusClient.disconnect during Activity.onStop() .

Your activity will listen for when the connection has established or failed by implementing the ConnectionCallbacks and OnConnectionFailedListener interfaces.

When the PlusClient object is unable to establish a connection, your implementation has an opportunity to recover inside your implementation of onConnectionFailed, where you are passed a connection status that can be used to resolve any connection failures. You should save this connection status in a member variable and invoke it by calling ConnectionResult.startResolutionForResult when the user presses the sign-in button or +1 button.

@Override
       public void onConnectionFailed(ConnectionResult result) {
   if (mConnectionProgressDialog.isShowing()) {
           // The user clicked the sign-in button already. Start to resolve
           // connection errors. Wait until onConnected() to dismiss the
           // connection dialog.
           if (result.hasResolution()) {
                   try {
                        result.startResolutionForResult(this,REQUEST_CODE_RESOLVE_ERR);
                   } catch (SendIntentException e) {
                           mPlusClient.connect();
                   }
           }
            }

   // Save the intent so that we can start an activity when the user clicks
   // the sign-in button.
   mConnectionResult = result;
   } 

      @Override
      public void onConnected() {
           // We've resolved any connection errors.
           mConnectionProgressDialog.dismiss();
            }

Because the resolution for the connection failure was started with startActivityForResult and the code REQUEST_CODE_RESOLVE_ERR, we can capture the result inside Activity.onActivityResult.

    protected void onActivityResult(int requestCode, int responseCode, Intent intent) {
if (requestCode == REQUEST_CODE_RESOLVE_ERR && responseCode == RESULT_OK) {
    mConnectionResult = null;
    mPlusClient.connect();
    }
    }

And example would be:

    import com.google.android.gms.common.*;
      import com.google.android.gms.common.GooglePlayServicesClient.*;
        import com.google.android.gms.plus.PlusClient;

      public class ExampleActivity extends Activity implements View.OnClickListener,
    ConnectionCallbacks, OnConnectionFailedListener {
private static final String TAG = "ExampleActivity";
private static final int REQUEST_CODE_RESOLVE_ERR = 9000;

private ProgressDialog mConnectionProgressDialog;
private PlusClient mPlusClient;
private ConnectionResult mConnectionResult;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
     //create an plusclient object 
    mPlusClient = new PlusClient.Builder(this, this, this)
            .setVisibleActivities("http://schemas.google.com/AddActivity", " http://schemas.google.com/BuyActivity")
            .build();
    // Progress bar to be displayed if the connection failure is not resolved.
    mConnectionProgressDialog = new ProgressDialog(this);
    mConnectionProgressDialog.setMessage("Signing in...");
}

@Override
protected void onStart() {
    super.onStart();
      //connect
    mPlusClient.connect();
}

@Override
protected void onStop() {
    super.onStop();
     //disconnect
    mPlusClient.disconnect();
}

@Override
public void onConnectionFailed(ConnectionResult result) {
    if (result.hasResolution()) {
        try {
            //start Solution for connectivity problems
            result.startResolutionForResult(this, REQUEST_CODE_RESOLVE_ERR);
        } catch (SendIntentException e) {
            mPlusClient.connect();
        }
    }
    // Save the result and resolve the connection failure upon a user click.
    mConnectionResult = result;
}

@Override
protected void onActivityResult(int requestCode, int responseCode, Intent intent) {
    if (requestCode == REQUEST_CODE_RESOLVE_ERR && responseCode == RESULT_OK) {
        mConnectionResult = null;
        //Try connect again 
       mPlusClient.connect();
    }
}

@Override
public void onConnected() {
    //Get account name
    String accountName = mPlusClient.getAccountName();
    Toast.makeText(this, accountName + " is connected.", Toast.LENGTH_LONG).show();
}

@Override
public void onDisconnected() {
    Log.d(TAG, "disconnected");
}
  }

You can also add a button in xml to sign in and set a listener in class with findViewById(R.id.sign_in_button).setOnClickListener(this); :

  <com.google.android.gms.common.SignInButton
android:id="@+id/sign_in_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

Upvotes: 0

Related Questions