Using such code it is possible to link my app and use account.
if (mGoogleApiClient == null) {
mGoogleApiClient = new GoogleApiClient.Builder(thi
EDIT
THIS METHOD IS DEPRECATED
Use like this
if (mGoogleApiClient != null)
{
if (mGoogleApiClient.isConnected())
{
Plus.AccountApi.clearDefaultAccount(mGoogleApiClient);
mGoogleApiClient.disconnect();
}
}
clearDefaultAccount()
doesn't work. For sign out and clear selected account use Auth.GoogleSignInApi.signOut()
like this:
private GoogleApiClient mGoogleApiClient;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Build a GoogleApiClient with access to the Google Sign-In API and the
// options specified by gso.
mGoogleApiClient = new GoogleApiClient.Builder(getApplicationContext()) //Use app context to prevent leaks using activity
//.enableAutoManage(this /* FragmentActivity */, connectionFailedListener)
.addApi(Auth.GOOGLE_SIGN_IN_API)
.build();
}
@Override
protected void onStart() {
super.onStart();
mGoogleApiClient.connect();
}
@Override
protected void onStop() {
super.onStop();
if (mGoogleApiClient.isConnected()) {
mGoogleApiClient.disconnect();
}
}
private void signOut() {
if (mGoogleApiClient.isConnected()) {
Auth.GoogleSignInApi.signOut(mGoogleApiClient);
mGoogleApiClient.disconnect();
mGoogleApiClient.connect();
}
}
I've modified a code snippet from the android docs. This is a complete code snippet that logs in and out; displaying the user name, email and profile image. This code snippet is well tested. The origonal doc can be found here: https://developers.google.com/identity/sign-in/android/sign-in See this doc for dependencies.
public class MainActivity extends AppCompatActivity implements View.OnClickListener, GoogleApiClient.OnConnectionFailedListener {
private ImageView imgUserAvatar;
private TextView lblName;
private TextView lblEmailID;
private SignInButton btnSignIn;
private Button btnSignOut;
private GoogleSignInOptions gso;
private GoogleSignInClient googleSignInClient;
private GoogleSignInAccount account;
private GoogleApiClient googleApiClient;
private static final int RC_SIGN_IN = 58;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
imgUserAvatar = findViewById(R.id.imgUserAvatar);
lblName = findViewById(R.id.lblName);
lblEmailID = findViewById(R.id.lblEmailID);
btnSignIn = findViewById(R.id.btnSignIn);
btnSignOut = findViewById(R.id.btnSignOut);
btnSignIn.setOnClickListener(this);
btnSignOut.setOnClickListener(this);
btnSignIn.setSize(SignInButton.SIZE_STANDARD);
imgUserAvatar.setVisibility(View.GONE);
lblName.setVisibility(View.GONE);
lblEmailID.setVisibility(View.GONE);
btnSignOut.setVisibility(View.GONE);
btnSignIn.setVisibility(View.VISIBLE);
gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
//.requestIdToken(RT_GOOGLE_SIGN_ID_ID)
.requestEmail()
.build();
googleSignInClient = GoogleSignIn.getClient(this, gso);
googleApiClient = new GoogleApiClient.Builder(this).enableAutoManage(this, this).addApi(Auth.GOOGLE_SIGN_IN_API, gso).build();
account = GoogleSignIn.getLastSignedInAccount(this);
updateUI(account);
}
@Override
public void onClick(View view) {
switch(view.getId()) {
case R.id.btnSignIn:
signIn();
break;
case R.id.btnSignOut:
signOut();
break;
}
}
private void signIn() {
Intent signInIntent = googleSignInClient.getSignInIntent();
startActivityForResult(signInIntent, RC_SIGN_IN);
}
private void signOut() {
Auth.GoogleSignInApi.signOut(googleApiClient).setResultCallback(new ResultCallback<Status>() {
@Override
public void onResult(@NonNull Status status) {
imgUserAvatar.setVisibility(View.GONE);
lblName.setVisibility(View.GONE);
lblEmailID.setVisibility(View.GONE);
btnSignOut.setVisibility(View.GONE);
btnSignIn.setVisibility(View.VISIBLE);
}
});
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
//Result returned from launching the Intent from GoogleSignInClient.getSignInIntent(...);
if(requestCode == RC_SIGN_IN) {
//The Task returned from this call is always completed, no need to attach
//a listener.
Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
handleSignInResult(task);
}
}
private void handleSignInResult(Task<GoogleSignInAccount> completedTask) {
try {
GoogleSignInAccount account = completedTask.getResult(ApiException.class);
//Signed in successfully, show authenticated UI.
updateUI(account);
}
catch(ApiException e) {
updateUI(null);
}
}
private void updateUI(final GoogleSignInAccount account) {
if(account != null) {
lblName.setText(account.getDisplayName());
lblEmailID.setText(account.getEmail());
lblName.setVisibility(View.VISIBLE);
lblEmailID.setVisibility(View.VISIBLE);
btnSignOut.setVisibility(View.VISIBLE);
btnSignIn.setVisibility(View.GONE);
final Bitmap[] bitmap = new Bitmap[1];
final Handler handler = new Handler(new Handler.Callback() {
@Override
public boolean handleMessage(Message message) {
imgUserAvatar.setImageBitmap(bitmap[0]);
imgUserAvatar.setVisibility(View.VISIBLE);
return true;
}
});
new Thread(new Runnable() {
@Override
public void run() {
InputStream in;
try {
in = new java.net.URL(account.getPhotoUrl().toString()).openStream();
bitmap[0] = BitmapFactory.decodeStream(in);
handler.sendEmptyMessage(0);
}
catch(IOException e) {
e.printStackTrace();
}
}
}).start();
}
else if(account == null) {
imgUserAvatar.setVisibility(View.GONE);
lblName.setVisibility(View.GONE);
lblEmailID.setVisibility(View.GONE);
btnSignOut.setVisibility(View.GONE);
btnSignIn.setVisibility(View.VISIBLE);
}//End if(account != null)
}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
//ToDo in context only used for signout.
}
}
Also you may find this video useful: https://www.youtube.com/watch?v=2PIaGpJMCNs&t=
I found this up-to-date solution:
if (mGoogleApiClient != null && mGoogleApiClient.isConnected()) {
mGoogleApiClient.clearDefaultAccountAndReconnect().setResultCallback(new ResultCallback<Status>() {
@Override
public void onResult(Status status) {
mGoogleApiClient.disconnect();
}
});
}
The usage of
Plus.AccountApi.clearDefaultAccount(mGoogleApiClient);
is deprecated
https://developers.google.com/android/reference/com/google/android/gms/plus/Account