2

我正在将 Google SSO 集成到我的 Android 应用中。我想知道何时提示用户选择帐户对话框并决定单击背景,因此该对话框被关闭。

选择帐户对话框由以下各项触发:

    @Override
    public void onConnected(Bundle connectionHint) {
        Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
        Activity activity = MyActivity.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。

4

1 回答 1

1

对话框启动代码:

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

这是你需要的还是我误会了你?

于 2016-09-12T08:31:57.777 回答