使用androidx BiometricPrompt實現(xiàn)指紋驗證功能
androidsdk版本大于29之后,使用FingerprintManagerCompat進行指紋驗證顯示被廢棄,F(xiàn)ingerprintManagerCompat的使用方法這里不再敘述。骨骼要求使用新的api去完成指紋驗證,當然,BiometricPrompt不僅能做指紋驗證,本文只講解怎么用BiometricPrompt做指紋驗證。
官方api:https://developer.android.google.cn/reference/androidx/biometric/package-summary?hl=zh-cn
首先導包
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接口,在每次進行指紋操作后都會回調(diào)這個方法,注意:要想AuthenticationCallback的方法生效,必須在runnable里面執(zhí)行runnable的run方法。
callback里面有三個回調(diào)方法,
1. onAuthenticationError(int errMsgId, CharSequence errString),指紋驗證錯誤會調(diào)用此方法,errMsgId的值對應BiometricPrompt里面的常量
2. onAuthenticationSucceeded(@NonNull @NotNull BiometricPrompt.AuthenticationResult result),指紋驗證成功后調(diào)用,通過result.getAuthenticationType獲取驗證成功的方式,參數(shù)類型自行查看。
3. onAuthenticationFailed() 識別失敗調(diào)用,具體調(diào)用時機不太清楚。??梢詤⒖脊俜轿臋n說法
顯示指紋驗證需要一個BiometricPrompt.PromptInfo參數(shù),會彈起一個彈窗進行顯示,使用builder的方式初始化,可以設置title,subTitle,description,NegativeButtonText,用法如下
new BiometricPrompt.PromptInfo.Builder().setTitle("title") .setSubtitle("subTitle") .setDescription("description") .setDeviceCredentialAllowed(false) .setNegativeButtonText("button").build()
需要注意的是setDeviceCredentialAllowed與setNegativeButtonText只能存在一個,即setNegativeButtonText不為空setDeviceCredentialAllowed必須為false
驗證設備是否開啟指紋通過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 "); } }
到此這篇關于使用androidx BiometricPrompt實現(xiàn)指紋驗證的文章就介紹到這了,更多相關androidx指紋驗證內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Android BottomSheet效果的兩種實現(xiàn)方式
這篇文章主要介紹了Android BottomSheet效果的兩種實現(xiàn)方式,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-08-08詳解Android(共享元素)轉(zhuǎn)場動畫開發(fā)實踐
本篇文章主要介紹了詳解Android(共享元素)轉(zhuǎn)場動畫開發(fā)實踐,具有一定的參考價值,感興趣的小伙伴們可以參考一下。2017-08-08使用android隱藏api實現(xiàn)亮度調(diào)節(jié)的方法
使用android隱藏api實現(xiàn)亮度調(diào)節(jié)的方法,需要的朋友可以參考一下2013-05-05