-->

How to Implement Smart Lock for Passwords to Andro

2019-05-11 02:35发布

问题:

Does anyone has knowledge about Smart Lock? How does it works?

I want to develop an application implementing Smart Lock for passwords in Android application.

I am following https://developers.google.com/identity/smartlock-passwords/android/.

I have initialized GoogleApiClient

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

and generated instance of Credential as

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

to save credentials using Credentials API, I used

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

my Manifest has the permission as

   <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" />

and also added Meta data inside application tag as

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

after doing all in Android application, I have generated project on Google Developer Console and have done everything as mentioned in this link here.

But when I run project, I am getting an error as:

Could not resolve error.

Has anyone worked on Google's Smart Lock for Password on Android application?

回答1:

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


回答2:

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.