我使用的是安卓 5.0。该版本提供 SmartLock 功能,允许通过连接受信任的设备来解锁密码/图案。我有一个注册为受信任设备的蓝牙低功耗 (BLE) 设备。我想使用 BLE 解锁(模式模式)手机。当BLE和手机连接并且事件可用数据时,它将解锁手机
if (BluetoothLeService.ACTION_DATA_AVAILABLE.equals(action))
// Calling unlock by the SmartLock API
我使用的是安卓 5.0。该版本提供 SmartLock 功能,允许通过连接受信任的设备来解锁密码/图案。我有一个注册为受信任设备的蓝牙低功耗 (BLE) 设备。我想使用 BLE 解锁(模式模式)手机。当BLE和手机连接并且事件可用数据时,它将解锁手机
if (BluetoothLeService.ACTION_DATA_AVAILABLE.equals(action))
// Calling unlock by the SmartLock API
这可能有点复杂,但谷歌已经大量提供了关于这种用法的文档。
要请求存储的凭证,您必须创建一个GoogleApiClient
配置为访问凭证 API 的实例。
mCredentialsApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.enableAutoManage(this, this)
.addApi(Auth.CREDENTIALS_API)
.build();
CredentialRequest 对象指定您要从中请求凭据的登录系统。CredentialRequest
使用setPasswordLoginSupported
基于密码登录的setAccountTypes()
方法,以及联合登录服务(如 Google Sign-In)的方法构建一个。
mCredentialRequest = new CredentialRequest.Builder()
.setPasswordLoginSupported(true)
.setAccountTypes(IdentityProviders.GOOGLE, IdentityProviders.TWITTER)
.build();
创建GoogleApiClient
和CredentialRequest
对象后,将它们传递给CredentialsApi.request()
方法以请求为您的应用存储的凭据。
Auth.CredentialsApi.request(mCredentialsClient, mCredentialRequest).setResultCallback(
new ResultCallback<CredentialRequestResult>() {
@Override
public void onResult(CredentialRequestResult credentialRequestResult) {
if (credentialRequestResult.getStatus().isSuccess()) {
// See "Handle successful credential requests"
onCredentialRetrieved(credentialRequestResult.getCredential());
} else {
// See "Handle unsuccessful and incomplete credential requests"
resolveResult(credentialRequestResult.getStatus());
}
}
});
在成功的凭据请求上,使用生成的凭据对象来完成用户对您的应用程序的登录。使用该getAccountType()
方法确定检索到的凭据的类型,然后完成相应的登录过程。
private void onCredentialRetrieved(Credential credential) {
String accountType = credential.getAccountType();
if (accountType == null) {
// Sign the user in with information from the Credential.
signInWithPassword(credential.getId(), credential.getPassword());
} else if (accountType.equals(IdentityProviders.GOOGLE)) {
// The user has previously signed in with Google Sign-In. Silently
// sign in the user with the same ID.
// See https://developers.google.com/identity/sign-in/android/
GoogleSignInOptions gso =
new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestEmail()
.build();
mGoogleApiClient = new GoogleApiClient.Builder(this)
.enableAutoManage(this, this)
.addApi(Auth.GOOGLE_SIGN_IN_API, gso)
.setAccountName(credential.getId())
.build();
OptionalPendingResult<GoogleSignInResult> opr =
Auth.GoogleSignInApi.silentSignIn(mGoogleApiClient);
// ...
}
}
我认为没有 SmartLock API。就像 Pravin 在评论中所说,智能锁会在设备连接时自动禁用图案。
我还没有尝试过,但是一旦禁用该模式,您应该可以使用以下方法绕过锁定屏幕(来自此答案):
KeyguardManager keyguardManager = (KeyguardManager) getSystemService(Activity.KEYGUARD_SERVICE);
KeyguardLock lock = keyguardManager.newKeyguardLock(KEYGUARD_SERVICE);
lock.disableKeyguard();
您需要向清单添加权限:
<uses-permission android:name="android.permission.DISABLE_KEYGUARD"/>