Android Smart Lock setResultCallback 未被调用

Android Smart Lock setResultCallback is not getting called

我一直在关注 https://developers.google.com/identity/smartlock-passwords/android/retrieve-credentials 以尝试在用户已将其凭据保存到 chrome 中的新 Android 智能锁功能时自动登录用户。我完全按照指南进行操作,但是我传递给 setResultCallback() 的回调没有被调用。有人 运行 以前遇到过这个问题吗?

没有错误消息或任何错误消息,只是没有被调用。

我厌倦了官方演示应用程序 here,它确实有效。

基本上,setResultCallback() 将在 saverequestdelete

时被调用

保存:

Auth.CredentialsApi.save(mCredentialsApiClient, credential).setResultCallback(
                new ResultCallback<Status>() {
                    @Override
                    public void onResult(Status status) {
                        if (status.isSuccess()) {
                            Log.d(TAG, "SAVE: OK");
                            showToast("Credential Saved");
                            hideProgress();
                        } else {
                            resolveResult(status, RC_SAVE);
                        }
                    }
                });

要求:

Auth.CredentialsApi.request(mCredentialsApiClient, request).setResultCallback(
                new ResultCallback<CredentialRequestResult>() {
                    @Override
                    public void onResult(CredentialRequestResult credentialRequestResult) {
                        if (credentialRequestResult.getStatus().isSuccess()) {
                            // Successfully read the credential without any user interaction, this
                            // means there was only a single credential and the user has auto
                            // sign-in enabled.
                            processRetrievedCredential(credentialRequestResult.getCredential(), false);
                            hideProgress();
                        } else {
                            // Reading the credential requires a resolution, which means the user
                            // may be asked to pick among multiple credentials if they exist.
                            Status status = credentialRequestResult.getStatus();
                            if (status.getStatusCode() == CommonStatusCodes.SIGN_IN_REQUIRED) {
                                // This is a "hint" credential, which will have an ID but not
                                // a password.  This can be used to populate the username/email
                                // field of a sign-up form or to initialize other services.
                                resolveResult(status, RC_HINT);
                            } else {
                                // This is most likely the case where the user has multiple saved
                                // credentials and needs to pick one
                                resolveResult(status, RC_READ);
                            }
                        }
                    }
                });

要删除:

Auth.CredentialsApi.delete(mCredentialsApiClient, mCurrentCredential).setResultCallback(
                new ResultCallback<Status>() {
                    @Override
                    public void onResult(Status status) {
                        hideProgress();
                        if (status.isSuccess()) {
                            // Credential delete succeeded, disable the delete button because we
                            // cannot delete the same credential twice.
                            showToast("Credential Delete Success");
                            findViewById(R.id.button_delete_loaded_credential).setEnabled(false);
                            mCurrentCredential = null;
                        } else {
                            // Credential deletion either failed or was cancelled, this operation
                            // never gives a 'resolution' so we can display the failure message
                            // immediately.
                            Log.e(TAG, "Credential Delete: NOT OK");
                            showToast("Credential Delete Failed");
                        }
                    }
                });

您也可以在我的 github here, set the SHA1 in your console here.

中克隆该项目

此时你应该准备好了:)

问题可能是Google API客户端没有连接上,尝试在你的activity的onStart()方法中调用connect(),或者如果您使用的是最新版本的 Play 服务,我们添加了 API 客户端的自动管理功能,使这变得更容易,真正简化了事情并避免了常见问题。

在构建 GoogleApiClient 时调用 enableAutoManage():

    // "this" is a reference to your activity
    mCredentialsApiClient = new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(this)
            .enableAutoManage(this, this)
            .addApi(Auth.CREDENTIALS_API)
            .build();

然后您可以发出 API 请求而无需在任何时候调用 mCredentialsApiClient.onConnect(),Google API 客户端的生命周期将自动为您管理。例如

@Override
public void onStart() {
    CredentialRequest request = new CredentialRequest.Builder()
            .setSupportsPasswordLogin(true)
            .build();
    Auth.CredentialsApi.request(mCredentialsApiClient, request).setResultCallback(
            new ResultCallback<CredentialRequestResult>() {
                public void onResult(CredentialRequestResult result) {
                    // result.getStatus(), result.getCredential() ... sign in automatically!
...

在 Github 查看完整的示例应用程序:https://github.com/googlesamples/android-credentials/blob/master/credentials-quickstart/app/src/main/java/com/google/example/credentialsbasic/MainActivity.java