有人知道智能锁吗?
它是如何工作的?
我想开发一个在android应用程序中实现密码智能锁的应用程序。
我正在跟踪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
}
}
}
}
});
My
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
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
在android应用程序中完成所有工作之后,我在
Meta data
上生成了项目,并完成了this link here中提到的所有工作。但是当我运行project时,我得到一个错误:
无法解决错误。
有人在安卓应用程序上开发过谷歌智能密码锁吗?
最佳答案
对我来说,Smart Lock for Passwords on Android示例项目工作得很好。你可能想试着从那里开始,看看是否有帮助。在示例代码中,save credential实现为:
在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();
}
}