How to Implement Smart Lock for Passwords to Android Application

こ雲淡風輕ζ 提交于 2019-12-05 18:45:44

For me, the Smart Lock for Passwords on Android sample project worked fine. You might want to try starting from there and see if that helps. In the sample code, save credential is implemented as:

In onCreate:

    mCredentialsApiClient = new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(Auth.CREDENTIALS_API)
            .build();

... In the saveCredetial method:

    final Credential credential = new Credential.Builder(email)
            .setPassword(password)
            .build();

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

... The resolveResult method on the main activity:

private void resolveResult(Status status, int requestCode) {
    Log.d(TAG, "Resolving: " + status);
    if (status.hasResolution()) {
        Log.d(TAG, "STATUS: RESOLVING");
        try {
            status.startResolutionForResult(MainActivity.this, requestCode);
        } catch (IntentSender.SendIntentException e) {
            Log.e(TAG, "STATUS: Failed to send resolution.", e);
            hideProgress();
        }
    } else {
        Log.e(TAG, "STATUS: FAIL");
        showToast("Could Not Resolve Error");
        hideProgress();
    }
}

Do you handle the result in the onActivityResult(...) callback?

The naming is slightly confusing here. You've implemented the onResult(Status status) callback method but that is not all the result handling you need. If your code ever hits the status.startResolutionForResult(this, RC_SAVE); line the result of that is not going to come to onResult(Status status) but rather to standard onActivityResult(...) callback where you need to handle it.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!