简体   繁体   English

如何在我的应用程序中使用智能锁API来解锁模式模式?

[英]How to use smart lock API in my application to unlock pattern mode?

I am using Android 5.0. 我使用的是Android 5.0。 The version provides the SmartLock function which allows unlocking the password/pattern by connecting with a trusted device. 该版本提供SmartLock功能,允许通过连接可信设备来解锁密码/模式。 I have a bluetooth low energy (BLE) device which registered as trusted device. 我有一个蓝牙低功耗(BLE)设备,注册为可信设备。 I want to use the BLE to unlock (pattern mode) the phone. 我想用BLE解锁(模式模式)手机。 It will unlock the phone when the BLE and phone are connected and data is available by the event 当BLE和手机连接并且事件可用数据时,它将解锁手机

if (BluetoothLeService.ACTION_DATA_AVAILABLE.equals(action)) 
// Calling unlock by the SmartLock API

If anyone who worked with SmartLock, please give me some guidance to do it? 如果有人使用SmartLock,请给我一些指导吗? I did not find any SmartLock API to do it. 我没有找到任何SmartLock API来做到这一点。 在此输入图像描述

There is no external API as such for SmartLock. SmartLock没有外部API。 You can check Google Docs for reference on it. 您可以查看Google 文档以获取参考。

You can check this sample out on GitHub and here you will find the tutorial on how to integrate smart lock API in your app. 您可以在GitHub上查看示例,在这里您将找到有关如何在您的应用中集成智能锁API的教程。

It might be a little bit complicated, but Google already huge provided Docs about this usage. 它可能有点复杂,但谷歌已经提供了大量有关此用法的文档

To request stored credentials, you must create an instance of GoogleApiClient configured to access the Credentials API. 要请求存储的凭据,您必须创建一个配置为访问Credentials API的GoogleApiClient实例。

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

A CredentialRequest object specifies the sign-in systems from which you want to request credentials. CredentialRequest对象指定要从中请求凭据的登录系统。 Build a CredentialRequest using the setPasswordLoginSupported method for password-based sign-in, and the setAccountTypes() method for federated sign-in services such as Google Sign-In. 使用setPasswordLoginSupported方法为基于密码的登录构建CredentialRequest ,并为联合登录服务(如Google登录setAccountTypes()方法。

mCredentialRequest = new CredentialRequest.Builder()
    .setPasswordLoginSupported(true)
    .setAccountTypes(IdentityProviders.GOOGLE, IdentityProviders.TWITTER)
    .build();

After you have created GoogleApiClient and CredentialRequest objects, pass them to the CredentialsApi.request() method to request credentials stored for your app. 创建GoogleApiClientCredentialRequest对象后,将它们传递给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());
            }
        }
    });

On a successful credential request, use the resulting Credential object to complete the user's sign-in to your app. 在成功的凭据请求中,使用生成的凭据对象完成用户对您的应用程序的登录。 Use the getAccountType() method to determine the type of retrieved credentials, then complete the appropriate sign-in process. 使用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);
        // ...
    }
}

I don't think there is a SmartLock API. 我认为没有SmartLock API。 Like Pravin said in the comments, smart lock will automatically disable the pattern when the device is connected. 就像Pravin在评论中所说,智能锁将在设备连接时自动禁用模式。

I haven't tried this, but once the pattern is disabled you should be able to bypass the lock screen with the following (from this answer ): 我没有试过这个,但是一旦模式被禁用,你应该能够绕过锁定屏幕(从这个答案 ):

KeyguardManager keyguardManager = (KeyguardManager) getSystemService(Activity.KEYGUARD_SERVICE);
KeyguardLock lock = keyguardManager.newKeyguardLock(KEYGUARD_SERVICE);
lock.disableKeyguard();

You will need to add a permission to your manifest: 您需要为清单添加权限:

<uses-permission android:name="android.permission.DISABLE_KEYGUARD"/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM