How to integrate google+ sign in my android app?

前端 未结 4 1250
天涯浪人
天涯浪人 2021-02-06 15:19

Hello all actually i need to sign in people from google+ through my app now i read documentation on google where it isstated that

To allow users to sign

4条回答
  •  执念已碎
    2021-02-06 16:00

    This is the code
    
    
      public class MainActivity extends AppCompatActivity implements View.OnClickListener, GoogleApiClient.OnConnectionFailedListener { private static final String TAG = MainActivity.class.getSimpleName(); private static final int RC_SIGN_IN = 007; private GoogleApiClient mGoogleApiClient; private ProgressDialog mProgressDialog; private Button btnSignIn; private Button btnSignOut; private LinearLayout llProfileLayout; private ImageView imgProfilePic; private TextView txtName, txtEmail; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btnSignIn = (Button)findViewById(R.id.login); btnSignOut = (Button) findViewById(R.id.logout); imgProfilePic = (ImageView) findViewById(R.id.pic); txtName = (TextView) findViewById(R.id.name); txtEmail = (TextView) findViewById(R.id.email); btnSignIn.setOnClickListener(this); btnSignOut.setOnClickListener(this); GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN).requestEmail().build(); mGoogleApiClient = new GoogleApiClient.Builder(this).enableAutoManage(this, this).addApi(Auth.GOOGLE_SIGN_IN_API, gso).build(); } private void signIn() { Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient); startActivityForResult(signInIntent, RC_SIGN_IN); } private void signOut() { Auth.GoogleSignInApi.signOut(mGoogleApiClient).setResultCallback(new ResultCallback() { @Override public void onResult(Status status) { updateUI(false); } }); } private void revokeAccess() { Auth.GoogleSignInApi.revokeAccess(mGoogleApiClient).setResultCallback(new ResultCallback() { @Override public void onResult(Status status) { updateUI(false); } }); } private void handleSignInResult(GoogleSignInResult result) { Log.d(TAG, "handleSignInResult:" + result.isSuccess()); if (result.isSuccess()) { /* Signed in successfully, show authenticated UI.*/ GoogleSignInAccount acct = result.getSignInAccount();
        Log.e(TAG, "display name: " + acct.getDisplayName());
    
        String personName = acct.getDisplayName();
        String personPhotoUrl = acct.getPhotoUrl().toString();
        String email = acct.getEmail();
    
    
        Log.e(TAG, "Name: " + personName + ", email: " + email
                + ", Image: " + personPhotoUrl);
    
        txtName.setText(personName);
        txtEmail.setText(email);
        ;
    } else {
    
        updateUI(false);
    }
    }
    
        @Override
        public void onClick(View v) {
            int id = v.getId();
    
            switch (id) {
                case R.id.login:
                    signIn();
                    break;
    
                case R.id.logout:
                    signOut();
                    break;
    
            }
        }
    
        @Override
        public void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
    
                    if (requestCode == RC_SIGN_IN) {GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
                Log.d(TAG, "onActivityResult: "+result);
                handleSignInResult(result);
    
            }
        }
    
        @Override
        public void onStart() {
            super.onStart();
    
            OptionalPendingResult opr = Auth.GoogleSignInApi.silentSignIn(mGoogleApiClient);
            if (opr.isDone()) {
            Log.d(TAG, "Got cached sign-in");
                GoogleSignInResult result = opr.get();
    
            }
        }
    
        @Override
        public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
    
            Log.d(TAG, "onConnectionFailed:" + connectionResult);
        }
    
    
    
        private void updateUI(boolean isSignedIn) {
            if (isSignedIn) {
                btnSignIn.setVisibility(View.GONE);
                btnSignOut.setVisibility(View.VISIBLE);
                llProfileLayout.setVisibility(View.VISIBLE);
            } else {
                btnSignIn.setVisibility(View.VISIBLE);
                btnSignOut.setVisibility(View.GONE);
    
            }
        }
    }
    

提交回复
热议问题