I've been dealing with Google+ login on android lately, and one thing keeps bugging me.
In all of their officially sanctioned examples, there isn't a method that specifically shows the Login process. The method that is called every time you try to log someone in is called resolveSignInError() shown here:
private void resolveSignInError() {
if (mConnectionResult.hasResolution()) {
try {
mIntentInProgress = true;
mConnectionResult.startResolutionForResult(this, RC_SIGN_IN);
Log.d("mGoogleApiClient ", mGoogleApiClient.toString());
} catch (IntentSender.SendIntentException e) {
mIntentInProgress = false;
mGoogleApiClient.connect();
}
}
}
This method also relies on the fact that there is a mConnectionResult, which is set in the on connection failure method
public void onConnectionFailed(ConnectionResult result) {
if (!result.hasResolution()) {
GooglePlayServicesUtil.getErrorDialog(result.getErrorCode(), this,
0).show();
return;
}
if (!mIntentInProgress) {
// Store the ConnectionResult for later usage
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();
}
}
}
So the entire scheme revolves around having already failed to login at least once. Does anyone know why there isn't a method that directly logs you into google plus? Why does this make sense as a way to structure readable software?
The reason is that that the device can't know whether you actually need to log in or not - sign in is really more like accessing a facility (as you might with location) than it is entering a username and password. The basic flow is:
If you then want to log in, you go through the connection result resolution.
The subtle thing is that you may go to step 2 even if the user has never used the app on the device before! So, for example, if I go to your website and sign in there, then install your android app.
So the reason that there is no login method is that the login status is actually remote - it lives on the Google servers, not on the device itself. The flow therefore has to be asynchronous.
Here is pretty normal example https://developers.google.com/+/mobile/android/sign-in:
There is method
first steps are there https://developers.google.com/+/mobile/android/getting-started
I am aware that this thread is very old. Updating as this comes up every time on Google. Now that google play store API v 8.4 is released, onConnectionFailed does exactly what it has to do.
Check this link https://github.com/googlesamples/google-services/blob/master/android/signin/app/src/main/java/com/google/samples/quickstart/signin/SignInActivity.java