Android Google Sign in Exceptions

Gourav :

I want to get the exceptions caused when a user fails to log into my app using Google Sign in. For example, in the handleSignInResult(@NonNull Task<GoogleSignInAccount> completedTask) method I use this:

private void handleSignInResult(@NonNull Task<GoogleSignInAccount> completedTask) {
    try {
        account = completedTask.getResult(ApiException.class);
        if(isSignedIn()) {
            Toast.makeText(this, "Success!", Toast.LENGTH_SHORT).show();
            startActivity(new Intent(MainActivity.this, Home.class));
        }
    } catch (ApiException e) {
            Toast.make(this, "Failed to login because: " + e.getCause(), Toast.LENGTH_LONG).show();
        }
    }
}

This helps me to get the cause of the error. But the most common cause is that when when no account is selected, this code returns null.

But I want to make a Toast when no account is selcted:

Toast.makeText(this, "Failed to login because: No account selected!", Toast.LENGTH_LONG).show();

I think that this can be achieved using the switch case but I don't know how to do that.

Gourav :

This is what I should use:

private void handleSignInResult(@NonNull Task<GoogleSignInAccount> completedTask) {
    try {
        account = completedTask.getResult(ApiException.class);
        if(isSignedIn()) {
            Toasty.success(this, "Success!", Toast.LENGTH_SHORT, true).show();
            startActivity(new Intent(MainActivity.this, Home.class));
        }
    } catch (ApiException e) {
        if(e.getCause() != null) {
            Toast.makeText(this, "Failed to login because: " + e.getCause(), Toast.LENGTH_LONG).show();
        }
        else {
            Toast.makeText(this, "Failed to login because: No account Selected!", Toast.LENGTH_LONG).show();
        }
    }
}`

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=106408&siteId=1