How to sign out of google plus account in google plus android integration
I did the following work, but it doesn't work after logging out quickly and start again in the application
private void signOutFromGplus() {
if (mGoogleApiClient.isConnected()) {
// clearCookies();
Plus.AccountApi
.clearDefaultAccount(mGoogleApiClient);
.mGoogleApiClient.disconnect();
.mGoogleApiClient.connect();
}
}
source to share
Assign a variable in this class (The advantage of using this class is that this class is run when the application is running and you can access it anywhere in the application): MyApplication.class
import android.app.Application;
import android.support.v7.app.AppCompatActivity;
import com.google.android.gms.auth.api.Auth;
import com.google.android.gms.auth.api.signin.GoogleSignInOptions;
import com.google.android.gms.common.api.GoogleApiClient;
public class MyApplication extends Application {
private GoogleApiClient mGoogleApiClient;
private GoogleSignInOptions gso;
public AppCompatActivity activity;
public GoogleSignInOptions getGoogleSignInOptions(){
gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestEmail()
.build();
return gso;
}
public GoogleApiClient getGoogleApiClient(AppCompatActivity activity, GoogleApiClient.OnConnectionFailedListener listener){
this.activity = activity;
mGoogleApiClient = new GoogleApiClient.Builder(this)
.enableAutoManage(this.activity, listener)
.addApi(Auth.GOOGLE_SIGN_IN_API, getGoogleSignInOptions())
.build();
return mGoogleApiClient;
}
}
In login activity
@Override
protected void onStart() {
mGoogleApiClient.connect();
super.onStart();
OptionalPendingResult<GoogleSignInResult> opr = Auth.GoogleSignInApi.silentSignIn(mGoogleApiClient);
if (opr.isDone()) {
Log.d("TAG", "Got cached sign-in");
GoogleSignInResult result = opr.get();
handleSignInResult(result);
} else {
opr.setResultCallback(new ResultCallback<GoogleSignInResult>() {
@Override
public void onResult(GoogleSignInResult googleSignInResult) {
//hideProgressDialog();
handleSignInResult(googleSignInResult);
}
});
}
}
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
gso = ((MyApplication) getApplication()).getGoogleSignInOptions();
mGoogleApiClient = ((MyApplication) getApplication()).getGoogleApiClient(LoginActivity.this, this);
}
@Override
protected void onStop() {
mGoogleApiClient.disconnect();
super.onStop();
}
In Second Activity where you want to exit
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
mGoogleApiClient = ((MyApplication) getApplication()).getGoogleApiClient(HomeActivity.this, this);
btnLogout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Auth.GoogleSignInApi.signOut(mGoogleApiClient).setResultCallback(
new ResultCallback<Status>() {
@Override
public void onResult(Status status) {
Intent i = new Intent(HomeActivity.this, LoginActivity.class);
startActivity(i);
finish();
}
});
}
});
}
In your app-level build.gradle file, declare Google Play Services as a dependency:
dependencies {
classpath 'com.android.tools.build:gradle:2.0.0'
classpath 'com.google.gms:google-services:1.5.0-beta2'
}
Let the MyApplication class extend the default application class. Alternatively, open your manifest file and add this line of code to your application element.
android:name=".MyApplication"
source to share
This may be an old question, but I want to make it clear here that it is most likely knowledgeable who first entered a google account. They may face an issue when automatically logging into google account after calling the method below.
private void signOutFromGplus() {
if (mGoogleApiClient.isConnected()) {
// clearCookies();
Plus.AccountApi
.clearDefaultAccount(mGoogleApiClient);
.mGoogleApiClient.disconnect();
.mGoogleApiClient.connect();
}
}
Decision. Make sure your mGoogleApiClient is not disabled somewhere already. So why doesn't it go into this if loop.
Most likely in onStop () or onDestroy ();
Hope this helps someone.
source to share
There is a difference between Sign-out
and Revoke
. You seem to want to disconnect the user from the app, so the user will be prompted to log in again next time. If so, try the following (see Google+ for Android login for more details ):
// Prior to disconnecting, run clearDefaultAccount().
Plus.AccountApi.clearDefaultAccount(mGoogleApiClient);
Plus.AccountApi.revokeAccessAndDisconnect(mGoogleApiClient)
.setResultCallback(new ResultCallback<Status>() {
onResult(Status status) {
// mGoogleApiClient is now disconnected and access has been revoked.
// Trigger app logic to comply with the developer policies
}
});
source to share