3

有人了解智能锁吗?它是如何工作的?

我想开发一个在 Android 应用程序中为密码实现 Smart Lock 的应用程序。

我正在关注https://developers.google.com/identity/smartlock-passwords/android/

我已经初始化GoogleApiClient

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

并生成Credentialas的实例

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

为了保存凭据Credentials API,我使用了

Auth.CredentialsApi.save(mCredentialsClient, credential).setResultCallback(
 new ResultCallback() {
  @Override
  public void onResult(Status status) {
      if (status.isSuccess()) {
          // Credentials were saved
      } else {
          if (status.hasResolution()) {
              // Try to resolve the save request. This will prompt the user if
              // the credential is new.
              try {
                  status.startResolutionForResult(this, RC_SAVE);
              } catch (IntentSender.SendIntentException e) {
                  // Could not resolve the request
              }
          }
      }
  }
});

Manifest的权限为

   <uses-permission android:name="android.permission.USE_CREDENTIALS" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />

Meta data在应用程序标签内添加为

    <meta-data
        android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" />

在 Android 应用程序中完成所有操作后,我已经生成了项目Google Developer Console并完成了此处链接中提到的所有内容。

但是当我运行项目时,我收到一个错误:

无法解决错误。

有没有人在 Android 应用程序上使用过 Google 的 Smart Lock for Password?

4

2 回答 2

7

对我来说,Android示例项目上的 Smart Lock for Passwords 运行良好。您可能想尝试从那里开始,看看是否有帮助。在示例代码中,保存凭据实现为:

onCreate

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

...在saveCredetial方法中:

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

...resolveResult主要活动的方法:

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();
    }
}
于 2015-06-10T19:49:57.450 回答
0

你在onActivityResult(...)回调中处理结果吗?

这里的命名有点混乱。您已经实现了onResult(Status status)回调方法,但这并不是您需要的全部结果处理。如果您的代码遇到了该status.startResolutionForResult(this, RC_SAVE);行,那么结果将不会出现,onResult(Status status)而是会出现onActivityResult(...)在您需要处理它的标准回调中。

于 2015-06-26T21:25:28.033 回答