杨罗佳 杨罗佳 - 3 months ago 31
Android Question

How to monitor cancel state in the android Google login?

enter image description here

I refer to the following link to integrate Google login, this is the latest Google login.
https://developers.google.com/identity/sign-in/android/start?hl=zh-cn

But this version can't cancel state monitoring, just like the picture above, if the user clicks on a dialog outside of the area, how can I listen to cancel the event?

The official offer code is as follows:

private void handleSignInResult(GoogleSignInResult result) {
Log.d(TAG, "handleSignInResult:" + result.isSuccess());
if (result.isSuccess()) {
// Signed in successfully, show authenticated UI.
GoogleSignInAccount acct = result.getSignInAccount();
mStatusTextView.setText(getString(R.string.signed_in_fmt, acct.getDisplayName()));
updateUI(true);
} else {
// Signed out, show unauthenticated UI.
updateUI(false);
}
}


But how to listen to cancel?

Answer

Check out my example:

    Log.d("result=", String.valueOf(result));
    if (result.isSuccess()) {
        GoogleSignInAccount acct = result.getSignInAccount();

        try {
            Log.i("Signin", acct.getDisplayName());
        }
        catch (NullPointerException e) {
            Log.d(TAG, "Error retrieving some account information");
        }
    } else {
        Status status = result.getStatus();
        int statusCode = status.getStatusCode();
        Log.d("StatusCode", String.valueOf(statusCode));
        if (statusCode == GoogleSignInStatusCodes.SIGN_IN_CANCELLED) {

        }
        else if (statusCode == GoogleSignInStatusCodes.SIGN_IN_FAILED) {

        }
        else {

        }
    }

You can retrieve a statuscode with result.getStatus(); Now you can compare the statusCode in the else part with GoogleSignInStatusCodes.SIGN_IN_CANCELLED

Comments