Step 1:
use in app gradle-
compile 'com.google.android.gms:play-services-plus:8.4.0'
Step 2:
Entry in AndroidManifest
<meta-data android:name="com.google.android.geo.API_KEY"android:value="AIzaSyDLJ4gqadVVrjTLM1x-RfsCU_QQ_olY-N0" />and past the google-services.json in appStep 3:And open Activity where you want to implemet andjust implemet GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener,View.OnClickListener
Data members .......private GoogleApiClient mGoogleApiClient; private ConnectionResult mConnectionResult; private static final int RC_SIGN_IN = 0;private void signInWithGplus() { if (!mGoogleApiClient.isConnecting()) { mSignInClicked = true; mGoogleApiClient.connect(); Log.e("", " isConnected is :: " + isConnected); if (isConnected == false) { resolveSignInError(); updateUI(true); } else if (isConnected == true) { getProfileInformation(); updateUI(true); } } else { // Toast.makeText(this, "User is not connected!", Toast.LENGTH_LONG).show(); } } private void resolveSignInError() { if (mConnectionResult.hasResolution()) { try { mIntentInProgress = true; mConnectionResult.startResolutionForResult(this, RC_SIGN_IN); } catch (IntentSender.SendIntentException e) { mIntentInProgress = false; mGoogleApiClient.connect(); } } } private void updateUI(boolean isSignedIn) { if (isSignedIn) { // btnSignIn.setVisibility(View.GONE);// authButton.setVisibility(View.GONE);// btnSignOut.setVisibility(View.VISIBLE);} else { // btnSignIn.setVisibility(View.VISIBLE);// authButton.setVisibility(View.VISIBLE);// btnSignOut.setVisibility(View.GONE); } } public void signOutFromGplus() {if (mGoogleApiClient.isConnected()) { Plus.AccountApi.clearDefaultAccount(mGoogleApiClient); mGoogleApiClient.disconnect(); // mGoogleApiClient.connect();isConnected = false; } } @SuppressWarnings("unused") private void revokeGplusAccess() { if (mGoogleApiClient.isConnected()) { Plus.AccountApi.clearDefaultAccount(mGoogleApiClient); Plus.AccountApi.revokeAccessAndDisconnect(mGoogleApiClient).setResultCallback(new ResultCallback<Status>() { @Override public void onResult(Status arg0) { // Log.e(TAG, "User access revoked!");mGoogleApiClient.connect(); updateUI(false); } }); } } public static String personPhotoUrl = null; // Fetching user's information name,email , profile picprivate void getProfileInformation() { try { Log.e("", " get profile information called.."); if (Plus.PeopleApi.getCurrentPerson(mGoogleApiClient) != null) { Person currentPerson = Plus.PeopleApi.getCurrentPerson(mGoogleApiClient); String personName = currentPerson.getDisplayName(); String dob = currentPerson.getBirthday(); personPhotoUrl = currentPerson.getImage().getUrl(); String acId=currentPerson.getId(); sph.setString("PROFILEPIC", personPhotoUrl); sph.setBoolean("ISSOCIAL", true); String personGooglePlusProfile = currentPerson.getUrl(); String email = Plus.AccountApi.getAccountName(mGoogleApiClient); int gender = currentPerson.getGender(); Log.e("TaG", "Name: " + personName + ", plusProfile: " +personGooglePlusProfile + ", email: " + email + ", gender : " + gender); try { if (mSignInClicked) { socialMeadiaType = 2; loadingDialog.dismiss(); // Go for next Activity>............} } catch (Exception e) { Toast.makeText(this, "User Already Exist!", Toast.LENGTH_LONG).show(); loadingDialog.dismiss(); } } else { Toast.makeText(getApplicationContext(), "Person information is null",Toast.LENGTH_LONG).show(); } } catch (Exception e) { e.printStackTrace(); loadingDialog.dismiss(); // Tracking exception } } protected void onStart() { super.onStart(); mGoogleApiClient.connect(); //EasyTracker.getInstance(this).activityStart(this);} protected void onStop() { super.onStop(); if (mGoogleApiClient.isConnected()) { mGoogleApiClient.disconnect(); } //EasyTracker.getInstance(this).activityStop(this);} boolean isConnected = false; boolean isFirstLogInGoogle = true; @Overridepublic void onConnected(Bundle arg0) { // TODO Auto-generated method stub mSignInClicked = false; isConnected = true; // Toast.makeText(this, "User is connected!", Toast.LENGTH_LONG).show(); updateUI(true); if (isFirstLogInGoogle == true) { isFirstLogInGoogle = false; getProfileInformation(); } } @Overridepublic void onConnectionSuspended(int i) { mGoogleApiClient.connect(); isConnected = false; updateUI(false); } public void googleLogin() { mGoogleApiClient = new GoogleApiClient.Builder(this) .addConnectionCallbacks(this) .addOnConnectionFailedListener(this) .addApi(Plus.API) .addScope(Plus.SCOPE_PLUS_LOGIN).build(); if (sph.getBoolean("ISLOGIN", false) == false) signOutFromGplus(); } @Overridepublic void onConnectionFailed(ConnectionResult result) { // TODO Auto-generated method stub if (!result.hasResolution()) { return; } if (!mIntentInProgress) { mConnectionResult = result; if (mSignInClicked) { resolveSignInError(); } } } @Overridepublic void onClick(View v) { int id=v.getId(); switch (id) { case R.id.txtLoginWithGoogle: loadingDialog.show(); signInWithGplus(); break; } }@Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data);}
No comments:
Post a Comment