Android Smart Lock setResultCallback未被调用

时间:2015-08-05 16:55:06

标签: android google-play-services credentials google-smartlockpasswords

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

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

2 个答案:

答案 0 :(得分:4)

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

在构建enableAutoManage()

时,只需致电GoogleApiClient即可
    // "this" is a reference to your activity
    mCredentialsApiClient = new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(this)
            .enableAutoManage(this, this)
            .addApi(Auth.CREDENTIALS_API)
            .build();

然后,您无需在任何时候致电mCredentialsApiClient.onConnect()即可发出API请求,Google API客户端的生命周期将自动为您管理。 e.g。

@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

答案 1 :(得分:0)

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

基本上,setResultCallback()saverequest

会调用delete

保存:

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中克隆项目,在控制台here中设置SHA1

此时你应该准备好了:)