简体   繁体   中英

google+ android app does not log me out and misconfigured in login flow

i am new in android app and i am doing google+ integration with my android app but i am facing problem in logout from google account .i have seen many tutorials and also download many demo projects but all in vain. also the login flow is not up to the mark. below i am going to explain u the steps performed by the app after login button clicked. sorry i can't post snapshots of my app screens.

  1. click on signin button
  2. add a google account by selecting EXISTING
  3. fill email and password
  4. signing in ....
  5. google services COMMUNICATION check box tick.
  6. click next
  7. then it automatically return to the login button screen ie very first screen
  8. and then after some milliseconds automatically display consent screen where i click on a SIGNIN option
  9. then i return back to the login screen (very first screen) again
  10. and then automatically after a few milli seconds go to the next activity which i performed on onConnected()
  11. there i write if(mGoogleApiClient.isConnected()) logout button display but it does not, but when i write the if condition for not connected then logout button display
  12. and then if i click on logout button ,my app crashes(i know this is not right way to go)
  13. but anyways on clicking logout button i am unable to logout even disconnect() is applied and also cookies are not deleted.

here below is my code:

MainActivity.java

    package com.gem.google;
    import java.util.Arrays;
    import java.util.List;
    import android.content.Intent;
    import android.content.IntentSender.SendIntentException;
    import android.os.Bundle;
    import android.support.v4.app.FragmentActivity;
    import android.util.Log;
    import android.view.View;
    import android.view.View.OnClickListener;
    import com.gem.google.R;
    import com.google.android.gms.common.ConnectionResult;
    import com.google.android.gms.common.api.GoogleApiClient.ConnectionCallbacks;
    import com.google.android.gms.common.api.GoogleApiClient.OnConnectionFailedListener;
    import com.google.android.gms.common.SignInButton;
    import com.google.android.gms.common.api.GoogleApiClient;
    import com.google.android.gms.plus.Plus;

    public class MainActivity extends FragmentActivity  implements
    ConnectionCallbacks , OnConnectionFailedListener
    {
        /* Request code used to invoke sign in user interactions. */
        private static final int RC_SIGN_IN = 0;
        private boolean mSignInClicked;

        /* Store the connection result from onConnectionFailed callbacks so that we can
         * resolve them when the user clicks sign-in.
         */
        private ConnectionResult mConnectionResult;
        /* Client used to interact with Google APIs. */
        private GoogleApiClient mGoogleApiClient;
        private boolean mIntentInProgress;
        private SignInButton signinbt;
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
             mGoogleApiClient = build_GoogleApiClient();
             setContentView(R.layout.activity_main);
             signinbt=(SignInButton) findViewById(R.id.sign_in_button);
            signinbt.setOnClickListener(new OnClickListener() {
            public void onClick(View view) {
                  if (view.getId() == R.id.sign_in_button
                    && !mGoogleApiClient.isConnecting()) {
                    mSignInClicked = true;
                   resolveSignInError();
                  }
                }});
    }

                 private GoogleApiClient build_GoogleApiClient() {
                // When we build the GoogleApiClient we specify where connected and
                // connection failed callbacks should be returned, which Google APIs our
                // app uses and which OAuth 2.0 scopes our app requests.
                return new GoogleApiClient.Builder(this)
                    .addConnectionCallbacks(this)
                    .addOnConnectionFailedListener(this)
                    .addApi(Plus.API)
                    .addScope(Plus.SCOPE_PLUS_LOGIN)
                    .build();
            }
protected void onStart() {
            super.onStart();
            mGoogleApiClient.connect();
          }


         protected void onStop() {
            super.onStop();

                if (mGoogleApiClient.isConnected()) {
                  mGoogleApiClient.disconnect();
                }
              }

        @Override
        public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == RC_SIGN_IN) {
                if (resultCode != RESULT_OK) {
                  mSignInClicked = false;
                }

                mIntentInProgress = false;

                if (!mGoogleApiClient.isConnecting()) {
                  mGoogleApiClient.connect();
                }
              } 

            }

        private void resolveSignInError() {
              if (mConnectionResult.hasResolution()) {
                try {
                  mIntentInProgress = true;
                  startIntentSenderForResult(mConnectionResult.getResolution().getIntentSender(),
                      RC_SIGN_IN, null, 0, 0, 0);
                } catch (SendIntentException e) {
                  // The intent was canceled before it was sent.  Return to the default
                  // state and attempt to connect to get an updated ConnectionResult.
                  mIntentInProgress = false;
                  mGoogleApiClient.connect();
                }
              }
            }

        public void onConnectionFailed(ConnectionResult result) {
             if (!mIntentInProgress) {
                    // Store the ConnectionResult so that we can use it later when the user clicks
                    // 'sign-in'.
                    mConnectionResult = result;

                    if (mSignInClicked) {
                      // The user has already clicked 'sign-in' so we attempt to resolve all
                      // errors until the user is signed in, or they cancel.
                      resolveSignInError();
                }
              }
            }

            public void onConnected(Bundle connectionHint) {

                  mSignInClicked = false;
                  Intent i = new Intent(MainActivity.this,nextscreen.class);
                  startActivity(i);

            }

            public void onConnectionSuspended(int cause) {
                  mGoogleApiClient.connect();
                }

            public void onDisconnected() {
                // TODO Auto-generated method stub

                }
}

nextscren.java

    import android.widget.Button;
    import com.google.android.gms.common.api.GoogleApiClient;
    import com.google.android.gms.plus.Plus;
    import android.app.Activity;
    import android.content.Intent;
    import android.os.Bundle;
    //import android.util.Log;
    import android.view.View;
    import android.view.View.OnClickListener;
    //import android.widget.Button;
    public class nextscreen extends Activity  implements
    GoogleApiClient.ConnectionCallbacks   {
        private Button signoutbt;
        private GoogleApiClient mGoogleApiClient;
        @Override
        public void onCreate(Bundle savedInstanceState) {         
        super.onCreate(savedInstanceState); 
            mGoogleApiClient = new GoogleApiClient.Builder(this)
              .addConnectionCallbacks(this)
            //  .addOnConnectionFailedListener(this)
              .addApi(Plus.API)
              .addScope(Plus.SCOPE_PLUS_LOGIN)
              .build();
               setContentView(R.layout.screen_next);
                signoutbt=(Button) findViewById(R.id.btn_sign_out);
               signoutbt.setVisibility (View.GONE);
             if(mGoogleApiClient.isConnected())
              {
        signoutbt.setVisibility (View.VISIBLE);
            signoutbt.setOnClickListener(new OnClickListener() {
        public void onClick(View view) {
              if (view.getId() == R.id.btn_sign_out) {
                if (mGoogleApiClient.isConnected()) {
                  Plus.AccountApi.clearDefaultAccount(mGoogleApiClient);
              mGoogleApiClient.disconnect();
              mGoogleApiClient.connect();
      // CookieSyncManager.createInstance(mGoogleApiClient);
     //   CookieManager cookieManager = CookieManager.getInstance();
      //  cookieManager.removeAllCookie();
    //CookieSyncManager.getInstance().sync() ;
     signoutbt.setVisibility (View.GONE);
             Intent i = new Intent(nextscreen.this,MainActivity.class);
            startActivity(i);
                 // Toast.makeText(this, "User is logout!", Toast.LENGTH_LONG).show();
                }
              }
            }
            });
          }    
     }
public void onConnected(Bundle connectionHint) {
}

    public void onConnectionSuspended(int cause) {
             mGoogleApiClient.connect();
            }
}

The nextscreen activity is a class where the the control goes on connected after login and where i write the code for logout button.

so what i want is :

  1. proper logout ie login session must clear after logout so that on login again it ask for user id and password.
  2. after successful login it directly go to the nextscreen activitywithout coming back to the mainactivity and then enter loginscreen.
  3. step 7 at above should not be there ie before consent screen login screen does not appear.

    so please guys help me and tell me where i am wrong. and sorry for this long question but i am helpless and have no choice. please understand and suggest something.

Am glad to have finally resolved this issue after a long time of researching. Bellow is the method for SigningOut

private void signOutFromGplus() {
    if (mGoogleApiClient.isConnected()) {
        Plus.AccountApi.clearDefaultAccount(mGoogleApiClient);
        mGoogleApiClient.disconnect();
    }
}

After which at onPause of the fragment or activity, call the signOutFromGplus since all the details i need are already on my SharedPreference which i use all through my app.

@Override
public void onPause() {
    super.onPause();
    signOutFromGplus();
}

So on coming back to the fragment, its already SignedOut. So it wont automatically Login anymore.

Hope this helps.``

Regards

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM