0

I am trying to integrate google plus login with my application and I am partially succeded with it.I am able to login with google plus and I am able to fetch the email id of the logged in user.The only problem for me now is I need to implement logout from googleplus so that the app will redirect to the login page and it should show up the popup to select the id to login when user clicks on login button. I have gone through a lot of examples but in all they are implementing the login within the same activity.I am stuck up with this for almost an week with this issue. Kindly let me know how can I achieve the above. https://developers.google.com/+/mobile/android/sign-in this is the tutorial I followed for the login integration

This is my code in main activity

SharedPreferences shared = getSharedPreferences("MyPrefs", MODE_PRIVATE);
        String channel = (shared.getString("logedin", ""));
        Log.d("loggedin",channel);
        if(channel.equals("false")){
            revokeGplusAccess();
        }

and this is my revokeGplusAccess function

private void revokeGplusAccess() {

            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);
                        }

                    });
        }

And below is the code for my second activity where signout button is there

sharedpreferences = getSharedPreferences("MyPrefs", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedpreferences.edit();

        editor.putString("logedin", "false");

        editor.commit();
        Intent intent = new Intent(this, MainActivity.class);

        startActivity(intent);

Below is the logcat error

java.lang.IllegalStateException: GoogleApiClient must be connected.
            at com.google.android.gms.common.internal.zzu.zza(Unknown Source)
            at com.google.android.gms.plus.Plus.zzf(Unknown Source)
            at com.google.android.gms.internal.zzpa.clearDefaultAccount(Unknown Source)
            at com.example.rating.Products.onSignedOut(Products.java:272)
            at com.example.rating.Products.onClick(Products.java:139)
            at android.view.View.performClick(View.java:4278)
            at android.view.View$PerformClick.run(View.java:17429)
            at android.os.Handler.handleCallback(Handler.java:725)
            at android.os.Handler.dispatchMessage(Handler.java:92)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:5099)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:803)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:570)
            at dalvik.system.NativeStart.main(Native Method)
Mohammad Tauqir
  • 1,817
  • 1
  • 18
  • 53
  • see my answer here https://stackoverflow.com/questions/31956346/google-plus-login-from-one-activity-and-logout-from-second-activity-by-clicking/31957283#31957283 – King of Masses Aug 28 '15 at 11:42
  • I tried adding a sharedprefrence and storing loggedin status.when I click logout button it comes to mainactivity and there it checks for status and calls revoke access activity.but the app is crashing – Gopinath Krishnamoorthy Aug 28 '15 at 12:10
  • post your codes with logcat details some one will help – King of Masses Aug 28 '15 at 12:11

1 Answers1

0
Try this. Hope it solve your problem.

// Google client to interact with Google API
private GoogleApiClient mGoogleApiClient;
@Override
protected void onCreate(Bundle savedInstanceState) {

    ...........your code........

    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this).addApi(Plus.API)
            .addScope(Plus.SCOPE_PLUS_LOGIN).build();

    ...........your code........

}


protected void onStart() {
    super.onStart();
    mGoogleApiClient.connect();
}

protected void onStop() {
    super.onStop();
    if (mGoogleApiClient.isConnected()) {
        mGoogleApiClient.disconnect();
    }
}

/**
 * Sign-out from google method to be called in logout button click
 * */
private void signOutFromGplus() {
    if (mGoogleApiClient.isConnected()) {
        Plus.AccountApi.clearDefaultAccount(mGoogleApiClient);
        mGoogleApiClient.disconnect();
        mGoogleApiClient.connect();

        Intent i = new Intent(YourActivityName.this, YourLoginActivity.class);
        YourActivityName.this.startActivity(i);
        YourActivityName.this.finish();

    }
}


@Override
public void onConnectionSuspended(int arg0) {
    mGoogleApiClient.connect();
}
Mohammad Tauqir
  • 1,817
  • 1
  • 18
  • 53