With Google play services v9 we can easily sign in with google in our apps, now we have GoogleSignInOptions builder that is used to configure Google sign in api.
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestEmail()
.requestProfile()
.requestIdToken(lam_server_google_client_id)
.build();
Now we have to connect google api client in onStart() and disconnect in onStop Like this :
@Override
protected void onStop() {
super.onStop();
mGoogleApiClient = GoogleAuth.getGoogleApiClient();
if (mGoogleApiClient != null && mGoogleApiClient.isConnected())
mGoogleApiClient.disconnect();
}
@Override
protected void onStart() {
super.onStart();
try {
mGoogleApiClient = GoogleAuth.getGoogleApiClient();
if (mGoogleApiClient != null)
mGoogleApiClient.connect();
else {
Utils.configureGoogle(PhotoProofsActivityNew.this);
mGoogleApiClient.connect();
}
} catch (Throwable e) {
e.printStackTrace();
}
}
mGoogleApiClient = new GoogleApiClient.Builder(LoginActivity.this)
.enableAutoManage(LoginActivity.this, new GoogleApiClient.OnConnectionFailedListener() {
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
}
})
.addApi(Auth.GOOGLE_SIGN_IN_API, myGSO)
.build();
Then, when the sign-in button is clicked, start the sign-in intent:
Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
startActivityForResult(signInIntent, RC_SIGN_IN);
Then user ll select gmail account and on selecting user account we can access user profile and Finally, handle the activity result:
@Override public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// Result returned from launching the Intent from
// GoogleSignInApi.getSignInIntent(...);
if (requestCode == RC_SIGN_IN) {
GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
if (result.isSuccess()) {
GoogleSignInAccount acct = result.getSignInAccount();
// Get account information
mFullName = acct.getDisplayName();
mEmail = acct.getEmail();
}
}
}
And these are the play services that need to be declare in gradle file.
compile 'com.google.android.gms:play-services-auth:9.0.2'
compile 'com.google.android.gms:play-services-plus:9.0.2'
0 Comment(s)