有没有人知道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
}
}
}
}
});
我的清单获得了许可
<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
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
在Android应用程序中完成所有操作后,我在Google Developer Console上生成了项目,并完成了this link here中提到的所有操作.
但是当我运行项目时,我收到的错误是:
Could not resolve error.
有没有人在Android应用程序上使用Google的Smart Lock for Password?
解决方法:
对我来说,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();
}
}