如何检测解雇谷歌"选择帐户"对话

时间:2016-09-12 06:24:13

标签: android google-sso

我在我的Android应用中集成了Google SSO。 我想知道当用户被提示选择帐户对话框并决定点击背景时,对话框就被解除了。

选择帐户对话框由以下方式触发:

    @Override
    public void onConnected(Bundle connectionHint) {
        Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
        Activity activity = SlateMathV2Activity.getActivity();
        activity.startActivityForResult(signInIntent, RC_GET_TOKEN);    
    }

我使用onActivityResult,但我不知道如何区分登录失败和解除对话。

public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == RC_GET_TOKEN) {
        GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
        if (result.isSuccess()) {
            //do something
        } else {
            // This is where I get when sign in fails, or when user dismisses the dialog
        }
    }
}

非常感谢帮助, 谢谢, Giora。

1 个答案:

答案 0 :(得分:0)

Dialog启动代码:

 Intent intent = AccountPicker.newChooseAccountIntent(null, null,
                new String[] {"com.google", "com.google.android.legacyimap"},
                false, null, null, null, null);
        startActivityForResult(intent, RC_GET_TOKEN);

关于活动结果:

public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == RC_GET_TOKEN) {
        //result code contains 0 if dialog dismissed by clicking on background or pressing Cancel button 
        // or  -1 if OK button is clicked.Tested on Samsung galaxy S5 Android 6.0

        boolean dialogIsDismissed;
        if(resultCode ==0) dialogIsDismissed = true;
        if(resultCode ==-1) dialogIsDismissed = false;

        GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
        if (result.isSuccess()) {
            //do something
        } else {
            // This is where I get when sign in fails, or when user dismisses the dialog
        }
    }
}

这是你需要的还是我弄错了?