使用androidx BiometricPrompt實(shí)現(xiàn)指紋驗(yàn)證功能
androidsdk版本大于29之后,使用FingerprintManagerCompat進(jìn)行指紋驗(yàn)證顯示被廢棄,F(xiàn)ingerprintManagerCompat的使用方法這里不再敘述。骨骼要求使用新的api去完成指紋驗(yàn)證,當(dāng)然,BiometricPrompt不僅能做指紋驗(yàn)證,本文只講解怎么用BiometricPrompt做指紋驗(yàn)證。
官方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è)構(gòu)造方法參數(shù)基本一致,executor里面是一個(gè)runnable接口,在每次進(jìn)行指紋操作后都會回調(diào)這個(gè)方法,注意:要想AuthenticationCallback的方法生效,必須在runnable里面執(zhí)行runnable的run方法。
callback里面有三個(gè)回調(diào)方法,
1. onAuthenticationError(int errMsgId, CharSequence errString),指紋驗(yàn)證錯(cuò)誤會調(diào)用此方法,errMsgId的值對應(yīng)BiometricPrompt里面的常量
2. onAuthenticationSucceeded(@NonNull @NotNull BiometricPrompt.AuthenticationResult result),指紋驗(yàn)證成功后調(diào)用,通過result.getAuthenticationType獲取驗(yàn)證成功的方式,參數(shù)類型自行查看。
3. onAuthenticationFailed() 識別失敗調(diào)用,具體調(diào)用時(shí)機(jī)不太清楚。??梢詤⒖脊俜轿臋n說法
顯示指紋驗(yàn)證需要一個(gè)BiometricPrompt.PromptInfo參數(shù),會彈起一個(gè)彈窗進(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只能存在一個(gè),即setNegativeButtonText不為空setDeviceCredentialAllowed必須為false
驗(yàn)證設(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實(shí)現(xiàn)指紋驗(yàn)證的文章就介紹到這了,更多相關(guān)androidx指紋驗(yàn)證內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Android實(shí)現(xiàn)EditText內(nèi)容保存為Bitmap的方法
這篇文章主要介紹了Android實(shí)現(xiàn)EditText內(nèi)容保存為Bitmap的方法,涉及Android中saveBitmap方法的簡單使用技巧,需要的朋友可以參考下2016-01-01
Android擴(kuò)大View點(diǎn)擊范圍的方法
Android4.0設(shè)計(jì)規(guī)定的有效可觸摸的UI元素標(biāo)準(zhǔn)是48dp,轉(zhuǎn)化為一個(gè)物理尺寸約為9毫米。7~10毫米,這是一個(gè)用戶手指能準(zhǔn)確并且舒適觸摸的區(qū)域。本文將介紹Android擴(kuò)大View點(diǎn)擊范圍的方法2021-05-05
Android Studio 3.6中新的視圖綁定工具ViewBinding 用法詳解
這篇文章主要介紹了Android Studio 3.6中新的視圖綁定工具ViewBinding 用法,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-03-03
android實(shí)現(xiàn)點(diǎn)擊按鈕切換不同的fragment布局
這篇文章主要為大家詳細(xì)介紹了android實(shí)現(xiàn)點(diǎn)擊按鈕切換不同的fragment布局,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-12-12
Android 用Time和Calendar獲取系統(tǒng)當(dāng)前時(shí)間源碼分享(年月日時(shí)分秒周幾)
這篇文章主要介紹了Android 用Time和Calendar獲取系統(tǒng)當(dāng)前時(shí)間源碼分享,包括年月日時(shí)分秒周幾的源碼,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下2017-01-01
Android 列表倒計(jì)時(shí)的實(shí)現(xiàn)的示例代碼(CountDownTimer)
本篇文章主要介紹了Android 列表倒計(jì)時(shí)的實(shí)現(xiàn)的示例代碼(CountDownTimer),具有一定的參考價(jià)值,有興趣的可以了解一下2017-09-09
Android App中的GridView網(wǎng)格布局使用指南
GridView布局所實(shí)現(xiàn)的就是類似于九宮格的矩陣界面效果,下面整理了Android App中的GridView網(wǎng)格布局使用指南,包括分割線的添加與自定義GridView的實(shí)現(xiàn)等技巧,需要的朋友可以參考下2016-06-06

