Android Smart Lock setResultCallback not receiving call

I followed https://developers.google.com/identity/smartlock-passwords/android/retrieve-credentials to try to auto login if they saved their credentials for the new Android Smart Lock feature in chrome. I followed the guide exactly, but my callback that I pass to setResultCallback () is not being called. Has anyone encountered this problem before?

There is no error message or whatever, it just isn't called.

+3


source to share


2 answers


Probably the problem is because the Google API client is not connected, try calling connect()

in the method of onStart()

your activity or if you are using the newest version of Play Services, we added an automatic API client management to make it easier, really simplify things and avoid common problems.

Just call enableAutoManage()

on creation GoogleApiClient

:

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

      



Then you can make API request without calling mCredentialsApiClient.onConnect()

at any time, Google API client lifecycle will be automatically managed for you. eg.

@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!
...

      

Check out the full example app on Github: https://github.com/googlesamples/android-credentials/blob/master/credentials-quickstart/app/src/main/java/com/google/example/credentialsbasic/MainActivity.java

+4


source


I got tired of the official demo app here and it worked.

Basically, setResultCallback()

will be called when save

, request

anddelete

To save:

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

      

For request:



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

      

For removing:

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");
                        }
                    }
                });

      

Also you can clone the project in my github here , install SHA1

in console.

At this point, you should be ready to go :)

0


source







All Articles