如何为Android应用程序实施密码的智能锁定

时间:2015-06-02 10:05:56

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

有没有人了解Smart Lock? 它是如何运作的?

我想在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();

并生成Credential的实例

 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上生成了项目,并完成了this link here中提到的所有内容。

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

  

无法解决错误。

有没有人使用Google的Android应用程序密码智能锁?

2 个答案:

答案 0 :(得分:7)

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

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

答案 1 :(得分:0)

您是否在library(dplyr) df %>% group_by(subj) %>% mutate(odd_even = 2 - (row_number() %% 2)) 回调中处理结果?

这里的命名有点令人困惑。您已经实现了mDevice.findObject(By.res(BASIC_SAMPLE_PACKAGE, "editTextUserInput")) .setText(STRING_TO_BE_TYPED); 回调方法,但这不是您需要的所有结果处理。如果您的代码曾经到达onActivityResult(...)行,则结果不会发送到onResult(Status status),而是发送到需要处理它的标准status.startResolutionForResult(this, RC_SAVE);回调。