和你一起終身學(xué)習(xí),這里是程序員 Android
經(jīng)典好文推薦,通過閱讀本文,您將收獲以下知識點:
一、指紋類概述
二、指紋類權(quán)限
三、指紋類詳細介紹
四、指紋使用實戰(zhàn)
官方的指紋識別在Android 6.0引入,Android 6.0之前指紋識別由廠商自己定義。所以Android 6.0之前的指紋識別碎片化嚴重。
一、指紋類概述
1.FingerprintManager:Android 6.0引入,Android 9.0 廢棄。使用時需加入權(quán)限:permission USE_FINGERPRINT
注:包含檢查是否支持指紋,指紋比對是否成功等。支持包中的FingerprintManagerCompat類對其作了包裝和兼容處理。不做詳細介紹
2.BiometricManager:Android 9.0引入。其不僅包含指紋識別,還包含人臉識別等其他驗證方式。
其主要功能為:檢查當(dāng)前設(shè)備是否具有指紋識別的條件,例如:設(shè)備是否具有指紋識別的硬件,指紋是否已經(jīng)錄入,硬件是否可用等。
需要配合權(quán)限permission USE_BIOMETRIC使用
注:目前,該類只有指紋識別的相關(guān)API,以后可能會加入人臉識別等其他生物識別的相關(guān)API。
推薦使用AndroidX支持庫中的類,其已經(jīng)對Android 6.0-Android 11做了兼容性處理:
- 在Android 9 版本和之前版本中會調(diào)用
FingerprintManagerCompat- 在Android 10 版本開始會調(diào)用框架層
BiometricManager
3.BiometricPrompt:
其主要功能為:發(fā)起指紋驗證
需要配合權(quán)限permission USE_BIOMETRIC使用
4.BiometricPrompt.PromptInfo:
其主要功能為:驗證對話框
通過其內(nèi)部類BiometricPrompt.PromptInfo.Builder,采用創(chuàng)造者模式配置對話框。僅能配置少量參數(shù),例如:標(biāo)題,副標(biāo)題等。這意味著:
1.不分Android版本,對話框的樣式統(tǒng)一
2.無法自定義對話款樣式
二、指紋類權(quán)限
-
permission USE_FINGERPRINT:使用FingerprintManager及FingerprintManagerCompat類進行指紋識別時,需獲取此權(quán)限。
此權(quán)限為:Normal Permission
-
permission USE_BIOMETRIC:使用BiometricManager等類進行指紋識別時,需獲取此權(quán)限。當(dāng)使用AndroidX支持庫中的
BiometricManager時候不需要聲明此權(quán)限,因為支持庫中已經(jīng)添加此權(quán)限
三、指紋類詳細介紹
1.BiometricManager屬性和方法
是否可用的狀態(tài)碼
| 屬性 | 含義 |
|---|---|
BIOMETRIC_ERROR_HW_UNAVAILABLE(value:1) |
The user can't authenticate because the hardware is unavailable. Try again later (傳感器當(dāng)前不可用,清稍后再試) |
BIOMETRIC_ERROR_NONE_ENROLLED (value:11) |
The user can't authenticate because no biometric or device credential is enrolled.(信息沒有錄入,比如還沒錄入指紋) |
BIOMETRIC_ERROR_NO_HARDWARE (value:12) |
The user can't authenticate because there is no suitable hardware (e.g. no biometric sensor or no keyguard).(沒有合適的傳感器或者沒設(shè)置密碼,例如手機沒有指紋傳感器) |
BIOMETRIC_ERROR_SECURITY_UPDATE_REQUIRED (value:15) |
The user can't authenticate because a security vulnerability has been discovered with one or more hardware sensors. The affected sensor(s) are unavailable until a security update has addressed the issue.(傳感器存在已知的漏洞,在更新修復(fù)漏洞前,傳感器不可用) |
BIOMETRIC_ERROR_UNSUPPORTED(value:-2) |
The user can't authenticate because the specified options are incompatible with the current Android version.(設(shè)置的一些驗證條件,當(dāng)前手機的Android版本無法滿足) |
BIOMETRIC_STATUS_UNKNOWN(value:-1) |
Unable to determine whether the user can authenticate(不知道是否可以進行驗證。通常在舊版本的Android手機上出現(xiàn),當(dāng)出現(xiàn)這個錯誤是,仍然可以嘗試進行驗證) |
BIOMETRIC_SUCCESS(value:0) |
The user can successfully authenticate.(可以進行驗證) |
-
方法:
| 方法名 | 作用 | 返回值 |
|---|---|---|
canAuthenticate()(已廢棄)推薦使用canAuthenticate(int)
|
檢查傳感器是否可用。 | 是否可用的狀態(tài)碼 |
canAuthenticate (int authenticators) |
檢查傳感器是否可用。 | 是否可用的狀態(tài)碼 |
from(Context context)(靜態(tài)方法) |
創(chuàng)建BiometricManager實例 |
BiometricManager實例 |
在canAuthenticate (int authenticators)中 authenticators取值為:
-
BIOMETRIC_STRONG: 滿足第三類要求的生物識別傳感器 -
BIOMETRIC_WEAK:滿足第二類要求的生物識別傳感器 -
DEVICE_CREDENTIAL:滿足安全設(shè)備的要求 (PIN, pattern, or password)
一般來說級別越高,安全性越高。詳情:聲明您的應(yīng)用支持的身份驗證類型
一般采用:BIOMETRIC_WEAK
注意:Android 10(API 級別 29)及更低版本不支持以下身份驗證器類型組合:DEVICE_CREDENTIAL 和 BIOMETRIC_STRONG | DEVICE_CREDENTIAL。如需檢查 Android 10 及更低版本中是否存在 PIN 碼、解鎖圖案或密碼,請使用 KeyguardManager.isDeviceSecure()方法
2.BiometricPrompt屬性和方法
驗證的結(jié)果常用錯誤碼(不全):
| 常用錯誤碼 | 描述 |
|---|---|
ERROR_CANCELED( Value:5) |
取消驗證 |
ERROR_HW_UNAVAILABLE(value:1) |
目前不可用,稍后再試 |
ERROR_LOCKOUT(value:7) |
驗證失敗了5次,等到30秒后再試 |
ERROR_LOCKOUT_PERMANENT(value:9) |
觸發(fā)了ERROR_LOCKOUT太多次,生物驗證鎖定,在使用設(shè)備驗證(例如:密碼,圖案)解鎖前,不能再使用生物驗證 |
ERROR_NEGATIVE_BUTTON(value:13) |
點擊了negative button
|
ERROR_NO_SPACE(value:4) |
設(shè)備可用存儲空間不足 |
ERROR_TIMEOUT (value:3) |
驗證超時。超時時間與設(shè)備和傳感器類型有關(guān) |
ERROR_USER_CANCELED(value:10) |
用戶取消了驗證 |
- 方法:
| 方法名 | 功能 |
|---|---|
authenticate (BiometricPrompt.PromptInfo info, BiometricPrompt.CryptoObject crypto) |
展示驗證對話框,調(diào)用基于加密的身份驗證。ps:與第二類生物驗證和Android 11之前的設(shè)備驗證不兼容 |
authenticate (BiometricPrompt.PromptInfo info) |
展示驗證對話框,調(diào)用身份驗證 |
cancelAuthentication () |
取消身份驗證,隱藏驗證對話框。ps:在Android 10(API 29)之前的版本中,當(dāng)用戶使用設(shè)備憑據(jù)進行身份驗證時調(diào)用此方法無效 |
四、指紋使用實戰(zhàn)
4.1 第一步:引入支持庫
implementation "androidx.biometric:biometric:1.1.0"
4.2 第二步:檢查指紋硬件是否可用
/**
*返回值見上文的“是否可用的狀態(tài)碼”
*/
public int isFingerprintAvailable(Context context){
BiometricManager manager = BiometricManager.from(context);
return manager.canAuthenticate(BiometricManager.Authenticators.BIOMETRIC_WEAK);
}
4.3 第三步:開始驗證
/**
* 開始驗證
*
* @param activity
* @param callBack 驗證結(jié)果回調(diào)
*/
public void authenticate(FragmentActivity activity, BiometricPrompt.AuthenticationCallback callBack) {
BiometricPrompt.PromptInfo promptInfo = createUi();
BiometricPrompt prompt = new BiometricPrompt(activity, ContextCompat.getMainExecutor(activity), callBack);
prompt.authenticate(promptInfo);
}
private BiometricPrompt.PromptInfo createUi() {
return new BiometricPrompt.PromptInfo.Builder()
.setTitle("Register Fingerprint")
.setSubtitle("Pls Touch the sensor")
.setNegativeButtonText("Use App Password")
.build()
}
4.4 第四步:獲取驗證結(jié)果
authenticate(mView, new BiometricPrompt.AuthenticationCallback() {
/**
* 驗證過程中發(fā)生了錯誤
* @param errorCode
* @param errString
*/
@Override
public void onAuthenticationError(int errorCode, @NonNull CharSequence errString) {
switch (errorCode) {
case ERROR_USER_CANCELED:
UIUtils.toast("取消了指紋識別");
break;
case ERROR_LOCKOUT:
UIUtils.toast("失敗5次,已鎖定,請30秒后在試");
break;
case ERROR_LOCKOUT_PERMANENT:
UIUtils.toast("失敗次數(shù)太多,指紋驗證已鎖定,請改用密碼,圖案等方式解鎖");
case ERROR_NEGATIVE_BUTTON:
UIUtils.toast("點擊了negative button");
break;
case ERROR_NO_DEVICE_CREDENTIAL:
UIUtils.toast("尚未設(shè)置密碼,圖案等解鎖方式");
break;
case ERROR_NO_SPACE:
UIUtils.toast("可用空間不足");
break;
case ERROR_TIMEOUT:
UIUtils.toast("驗證超時");
break;
}
}
@Override
public void onAuthenticationSucceeded(@NonNull BiometricPrompt.AuthenticationResult result) {
UIUtils.toast("驗證成功");
}
/**
* 驗證失敗
* @param
*/
@Override
public void onAuthenticationFailed() {
super.onAuthenticationFailed();
UIUtils.toast("驗證失敗,請重試");
}
});
4.5 實現(xiàn)效果
參考鏈接:http://www.itdecent.cn/p/530c9fa31786
至此,本篇已結(jié)束。轉(zhuǎn)載網(wǎng)絡(luò)的文章,小編覺得很優(yōu)秀,歡迎點擊閱讀原文,支持原創(chuàng)作者,如有侵權(quán),懇請聯(lián)系小編刪除,歡迎您的建議與指正。同時期待您的關(guān)注,感謝您的閱讀,謝謝!