簡體   English   中英

如何在我的應用程序中使用智能鎖API來解鎖模式模式?

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

我使用的是Android 5.0。 該版本提供SmartLock功能,允許通過連接可信設備來解鎖密碼/模式。 我有一個藍牙低功耗(BLE)設備,注冊為可信設備。 我想用BLE解鎖(模式模式)手機。 當BLE和手機連接並且事件可用數據時,它將解鎖手機

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

如果有人使用SmartLock,請給我一些指導嗎? 我沒有找到任何SmartLock API來做到這一點。 在此輸入圖像描述

SmartLock沒有外部API。 您可以查看Google 文檔以獲取參考。

您可以在GitHub上查看示例,在這里您將找到有關如何在您的應用中集成智能鎖API的教程。

它可能有點復雜,但谷歌已經提供了大量有關此用法的文檔

要請求存儲的憑據,您必須創建一個配置為訪問Credentials API的GoogleApiClient實例。

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

CredentialRequest對象指定要從中請求憑據的登錄系統。 使用setPasswordLoginSupported方法為基於密碼的登錄構建CredentialRequest ,並為聯合登錄服務(如Google登錄setAccountTypes()方法。

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

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

在成功的憑據請求中,使用生成的憑據對象完成用戶對您的應用程序的登錄。 使用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"/>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM