使用androidx BiometricPrompt實現(xiàn)指紋驗證功能
androidsdk版本大于29之后,使用FingerprintManagerCompat進(jìn)行指紋驗證顯示被廢棄,F(xiàn)ingerprintManagerCompat的使用方法這里不再敘述。骨骼要求使用新的api去完成指紋驗證,當(dāng)然,BiometricPrompt不僅能做指紋驗證,本文只講解怎么用BiometricPrompt做指紋驗證。
官方api:https://developer.android.google.cn/reference/androidx/biometric/package-summary?hl=zh-cn
首先導(dǎo)包
implementation 'androidx.biometric:biometric:1.0.1'
然后它的構(gòu)造方法
1.BiometricPrompt(@NonNull FragmentActivity fragmentActivity,
@NonNull Executor executor, @NonNull AuthenticationCallback callback)
2. BiometricPrompt(@NonNull Fragment fragment,
@NonNull Executor executor, @NonNull AuthenticationCallback callback)
兩個構(gòu)造方法參數(shù)基本一致,executor里面是一個runnable接口,在每次進(jìn)行指紋操作后都會回調(diào)這個方法,注意:要想AuthenticationCallback的方法生效,必須在runnable里面執(zhí)行runnable的run方法。
callback里面有三個回調(diào)方法,
1. onAuthenticationError(int errMsgId, CharSequence errString),指紋驗證錯誤會調(diào)用此方法,errMsgId的值對應(yīng)BiometricPrompt里面的常量
2. onAuthenticationSucceeded(@NonNull @NotNull BiometricPrompt.AuthenticationResult result),指紋驗證成功后調(diào)用,通過result.getAuthenticationType獲取驗證成功的方式,參數(shù)類型自行查看。
3. onAuthenticationFailed() 識別失敗調(diào)用,具體調(diào)用時機不太清楚。??梢詤⒖脊俜轿臋n說法
顯示指紋驗證需要一個BiometricPrompt.PromptInfo參數(shù),會彈起一個彈窗進(jìn)行顯示,使用builder的方式初始化,可以設(shè)置title,subTitle,description,NegativeButtonText,用法如下
new BiometricPrompt.PromptInfo.Builder().setTitle("title")
.setSubtitle("subTitle")
.setDescription("description")
.setDeviceCredentialAllowed(false)
.setNegativeButtonText("button").build()
需要注意的是setDeviceCredentialAllowed與setNegativeButtonText只能存在一個,即setNegativeButtonText不為空setDeviceCredentialAllowed必須為false
驗證設(shè)備是否開啟指紋通過BiometricManager.from(context).canAuthenticate() == BiometricManager.BIOMETRIC_SUCCESS方法;
代碼展示:
private BiometricPrompt biometricPrompt;
private void startFinger(){
biometricPrompt = new BiometricPrompt(this, new Executor() {
@Override
public void execute(Runnable command) {
command.run();
}
}, new FingerCallBack());
biometricPrompt.authenticate( new BiometricPrompt.PromptInfo.Builder().setTitle("title")
.setSubtitle("subTitle")
.setDescription("description")
.setDeviceCredentialAllowed(false)
.setNegativeButtonText("button").build());
}
private void cancelFinger() {
if (biometricPrompt != null) {
biometricPrompt.cancelAuthentication();
}
}
private class FingerCallBack extends BiometricPrompt.AuthenticationCallback {
@Override
public void onAuthenticationError(int errMsgId, CharSequence errString) {
super.onAuthenticationError(errMsgId, errString);
Log.e("fingers", "onAuthenticationError");
}
@Override
public void onAuthenticationSucceeded(@NonNull @NotNull BiometricPrompt.AuthenticationResult result) {
super.onAuthenticationSucceeded(result);
cancelFinger();
Log.e("fingers", "識別成功 onAuthenticationSucceeded");
}
@Override
public void onAuthenticationFailed() {
super.onAuthenticationFailed();
Log.e("fingers", "onAuthenticationFailed ");
}
}
到此這篇關(guān)于使用androidx BiometricPrompt實現(xiàn)指紋驗證的文章就介紹到這了,更多相關(guān)androidx指紋驗證內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Android實現(xiàn)EditText內(nèi)容保存為Bitmap的方法
這篇文章主要介紹了Android實現(xiàn)EditText內(nèi)容保存為Bitmap的方法,涉及Android中saveBitmap方法的簡單使用技巧,需要的朋友可以參考下2016-01-01
Android Studio 3.6中新的視圖綁定工具ViewBinding 用法詳解
這篇文章主要介紹了Android Studio 3.6中新的視圖綁定工具ViewBinding 用法,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-03-03
android實現(xiàn)點擊按鈕切換不同的fragment布局
這篇文章主要為大家詳細(xì)介紹了android實現(xiàn)點擊按鈕切換不同的fragment布局,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-12-12
Android 用Time和Calendar獲取系統(tǒng)當(dāng)前時間源碼分享(年月日時分秒周幾)
這篇文章主要介紹了Android 用Time和Calendar獲取系統(tǒng)當(dāng)前時間源碼分享,包括年月日時分秒周幾的源碼,非常不錯,具有參考借鑒價值,需要的朋友參考下2017-01-01
Android 列表倒計時的實現(xiàn)的示例代碼(CountDownTimer)
本篇文章主要介紹了Android 列表倒計時的實現(xiàn)的示例代碼(CountDownTimer),具有一定的參考價值,有興趣的可以了解一下2017-09-09
Android App中的GridView網(wǎng)格布局使用指南
GridView布局所實現(xiàn)的就是類似于九宮格的矩陣界面效果,下面整理了Android App中的GridView網(wǎng)格布局使用指南,包括分割線的添加與自定義GridView的實現(xiàn)等技巧,需要的朋友可以參考下2016-06-06

