1

I am trying to find a good documented example of how to perform a login in my app using a google account.

Maybe I am looking in the wrong place, but i can't find anything in the android sdk docs. From what i understand its part of Google Services but still having problems find examples.

I also need to support if the user has more than 1 google account configured on the device to popup and ask which account to use.

Then on future loading of my app I would automatically login.

Can anyone point me in the right direction, or are there no examples available?

Thanks

Martin
  • 23,844
  • 55
  • 201
  • 327

1 Answers1

2

You probably want to use this guide:

https://developers.google.com/+/mobile/android/sign-in

From the guide: You must create a Google APIs Console project and initialize the PlusClient object.

Add the Google+ Sign-In button to your app

Add the SignInButton in your application's layout:

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

Initialize mPlusClient with the requested visible activities in your Activity.onCreate handler.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mPlusClient = new PlusClient.Builder(this, this, this)
            .setVisibleActivities("http://schemas.google.com/AddActivity", "http://schemas.google.com/BuyActivity")
            .build();
}

In the Android activity, register your button's OnClickListener to sign in the user when clicked:

findViewById(R.id.sign_in_button).setOnClickListener(this);

After the user has clicked the sign-in button, you should start to resolve any connection errors held in mConnectionResult. Possible connection errors include prompting the user to select an account, and granting access to your app.

@Override
public void onClick(View view) {
    if (view.getId() == R.id.sign_in_button && !mPlusClient.isConnected()) {
        if (mConnectionResult == null) {
            mConnectionProgressDialog.show();
        } else {
            try {
                mConnectionResult.startResolutionForResult(this, REQUEST_CODE_RESOLVE_ERR);
            } catch (SendIntentException e) {
                // Try connecting again.
                mConnectionResult = null;
                mPlusClient.connect();
            }
        }
    }
}

When the user has successfully signed in, your onConnected handler will be called. At this point, you are able to retrieve the user’s account name or make authenticated requests.

@Override
public void onConnected(Bundle connectionHint) {
    mConnectionProgressDialog.dismiss();
    Toast.makeText(this, "User is connected!", Toast.LENGTH_LONG).show();
}
powerj1984
  • 2,216
  • 1
  • 22
  • 34