簡體   English   中英

如何在 Android 中實現生物識別 API?

[英]How can I implement Biometric API in Android?

我想使用生物識別或密碼來鎖定/解鎖我的應用程序中的圖像。 生物特征 API 可以檢測生物特征,但“使用密碼”選項采用設備的密碼/密碼。 我希望用戶在應用內輸入密碼和他想要的任何密碼。

在您的應用程序中同時使用生物特征和密碼是一種常見的模式。 本質上,這個想法是在支持它的設備上使用生物識別技術,並在其他情況下使用帳戶/應用程序密碼,如下所示:

override fun onClick(view: View) {  // user clicks a button in your app to authenticate
   val promptInfo = createPromptInfo()
   if (BiometricManager.from(context)
               .canAuthenticate() == BiometricManager.BIOMETRIC_SUCCESS) {
       biometricPrompt.authenticate(promptInfo, cryptoObject)
   } else {
       loginWithPassword()
   }
}

此外,在創建 PromptInfo 時,您將執行.setNegativeButtonText(getString(R.string.prompt_info_use_app_password))然后在 onAuthenticationError onAuthenticationError()回調中執行

override fun onAuthenticationError(errorCode: Int, errString: CharSequence) {
   super.onAuthenticationError(errorCode, errString)
   Log.d(TAG, "$errorCode :: $errString")
   if(errorCode == BiometricPrompt.ERROR_NEGATIVE_BUTTON) {
       loginWithPassword() // Because negative button says use account/app password
   }
}

注意cryptoObject的使用。 這是因為密碼或生物特征認證本身並不會加密您的數據。 因此,如果您真的希望您的數據(在這種情況下是您的照片)是私密的,您必須對它們進行加密。

然后最后在onAuthenticationSucceeded()回調中,您將顯示您的數據

   override fun onAuthenticationSucceeded(result: BiometricPrompt.AuthenticationResult) {
       super.onAuthenticationSucceeded(result)
       Log.d(TAG, "Authentication was successful")
       // Proceed with viewing the private encrypted data.
       showEncryptedData(result.cryptoObject)
   }

免責聲明:我為 Android/Google 工作,特別是在生物識別方面。 我可以回答您的后續問題

我使用了否定按鈕。 我將否定按鈕的文本設置為“使用密碼”,並在 onAuthenticationError 回調方法中處理否定按鈕 onclick。

if (errorCode == BiometricPrompt.ERROR_NEGATIVE_BUTTON) {
    //show the in app password dialog
}

暫無
暫無

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

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