Android6.0指紋識別開發(fā)實(shí)例詳解
Android6.0指紋識別開發(fā)實(shí)例詳解
最近在做android指紋相關(guān)的功能,谷歌在android6.0及以上版本對指紋識別進(jìn)行了官方支持。當(dāng)時在FingerprintManager和FingerprintManagerCompat這兩個之間糾結(jié),其中使用FingerprintManager要引入com.android.support:appcompat-v7包,考慮到包的大小,決定使用v4兼容包FingerprintManagerCompat來實(shí)現(xiàn)。
主要實(shí)現(xiàn)的工具類FingerprintUtil:驗(yàn)證手機(jī)是否支持指紋識別方法callFingerPrintVerify(),主要驗(yàn)證手機(jī)硬件是否支持(6.0及以上),有沒有錄入指紋,然后有沒有開啟鎖屏密碼,開始驗(yàn)證對于識別成功,失敗可以進(jìn)行相應(yīng)的回調(diào)處理。
實(shí)例代碼:
public class FingerprintUtil{ private FingerprintManagerCompat mFingerprintManager; private KeyguardManager mKeyManager; private CancellationSignal mCancellationSignal; private Activity mActivity; public FingerprintUtil(Context ctx) { mActivity = (Activity) ctx; mFingerprintManager = FingerprintManagerCompat.from(mActivity); mKeyManager = (KeyguardManager) mActivity.getSystemService(Context.KEYGUARD_SERVICE); } public void callFingerPrintVerify(final IFingerprintResultListener listener) { if (!isHardwareDetected()) { return; } if (!isHasEnrolledFingerprints()) { if (listener != null) { listener.onNoEnroll(); } return; } if (!isKeyguardSecure()) { if (listener != null) { listener.onInSecurity(); } return; } if (listener != null) { listener.onSupport(); } if (listener != null) { listener.onAuthenticateStart(); } if (mCancellationSignal == null) { mCancellationSignal = new CancellationSignal(); } try { mFingerprintManager.authenticate(null, 0, mCancellationSignal, new FingerprintManagerCompat.AuthenticationCallback() { //多次嘗試都失敗會走onAuthenticationError,會停止響應(yīng)一段時間,提示嘗試次數(shù)過多,請稍后再試。 @Override public void onAuthenticationError(int errMsgId, CharSequence errString) { if (listener != null) listener.onAuthenticateError(errMsgId, errString); } //指紋驗(yàn)證失敗走此方法,例如小米前4次驗(yàn)證失敗走onAuthenticationFailed,第5次走onAuthenticationError @Override public void onAuthenticationFailed() { if (listener != null) listener.onAuthenticateFailed(); } @Override public void onAuthenticationHelp(int helpMsgId, CharSequence helpString) { if (listener != null) listener.onAuthenticateHelp(helpMsgId, helpString); } //當(dāng)驗(yàn)證的指紋成功時會回調(diào)此函數(shù),然后不再監(jiān)聽指紋sensor @Override public void onAuthenticationSucceeded(FingerprintManagerCompat.AuthenticationResult result) { if (listener != null) listener.onAuthenticateSucceeded(result); } }, null); } catch (Exception e) { e.printStackTrace(); } } /** * 是否錄入指紋,有些設(shè)備上即使錄入了指紋,但是沒有開啟鎖屏密碼的話此方法還是返回false * * @return */ private boolean isHasEnrolledFingerprints() { try { return mFingerprintManager.hasEnrolledFingerprints(); } catch (Exception e) { return false; } } /** * 是否有指紋識別硬件支持 * * @return */ public boolean isHardwareDetected() { try { return mFingerprintManager.isHardwareDetected(); } catch (Exception e) { return false; } } /** * 判斷是否開啟鎖屏密碼 * * @return */ private boolean isKeyguardSecure() { try { return mKeyManager.isKeyguardSecure(); } catch (Exception e) { return false; } } /** * 指紋識別回調(diào)接口 */ public interface IFingerprintResultListener { void onInSecurity(); void onNoEnroll(); void onSupport(); void onAuthenticateStart(); void onAuthenticateError(int errMsgId, CharSequence errString); void onAuthenticateFailed(); void onAuthenticateHelp(int helpMsgId, CharSequence helpString); void onAuthenticateSucceeded(FingerprintManagerCompat.AuthenticationResult result); } public void cancelAuthenticate() { if (mCancellationSignal != null) { mCancellationSignal.cancel(); mCancellationSignal = null; } } public void onDestroy() { cancelAuthenticate(); mKeyManager = null; mFingerprintManager = null; }
參考了一些資料,做了一些驗(yàn)證,得到一些結(jié)論:
1、當(dāng)指紋識別失敗后,會調(diào)用onAuthenticationFailed()方法,這時候指紋傳感器并沒有關(guān)閉,谷歌原生系統(tǒng)給了我們5次重試機(jī)會,也就是說,連續(xù)調(diào)用了4次onAuthenticationFailed()方法后,第5次會調(diào)用onAuthenticateError(int errMsgId, CharSequence errString)方法,此時errMsgId==7。
2、每次重新授權(quán),哪怕不去校驗(yàn),取消時會走onAuthenticateError(int errMsgId, CharSequence errString) 方法,其中errMsgId==5,
3、當(dāng)系統(tǒng)調(diào)用了onAuthenticationError()和onAuthenticationSucceeded()后,傳感器會關(guān)閉,只有我們重新授權(quán),再次調(diào)用authenticate()方法后才能繼續(xù)使用指紋識別功能。
4、兼容android6.0以下系統(tǒng)的話,不要使用FingerprintManagerCompat, 低于M的系統(tǒng)版本,F(xiàn)ingerprintManagerCompat無論手機(jī)是否有指紋識別模塊,均認(rèn)為沒有指紋識別,可以用FingerprintManager來做。
5、考慮到安全因素,最好authenticate(CryptoObject crypto, CancellationSignal cancel, int flags, AuthenticationCallback callback, Handler handler)時加入CryptoObject 。crypto這是一個加密類的對象,指紋掃描器會使用這個對象來判斷認(rèn)證結(jié)果的合法性。這個對象可以是null,但是這樣的話,就意味著app無條件信任認(rèn)證的結(jié)果,這個過程可能被攻擊,數(shù)據(jù)可以被篡改,這是app在這種情況下必須承擔(dān)的風(fēng)險。因此,建議這個參數(shù)不要置為null。這個類的實(shí)例化有點(diǎn)麻煩,主要使用javax的security接口實(shí)現(xiàn)。
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
相關(guān)文章
activitygroup 切換動畫效果如何實(shí)現(xiàn)
本文將詳細(xì)介紹activitygroup 切換動畫效果實(shí)現(xiàn)過程,需要聊解的朋友可以參考下2012-12-12Android-ViewModel和LiveData使用詳解
這篇文章主要介紹了Android-ViewModel和LiveData使用詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-03-03Android 消息機(jī)制以及handler的內(nèi)存泄露
這篇文章主要介紹了Android 消息機(jī)制以及handler的內(nèi)存泄露的相關(guān)資料,需要的朋友可以參考下2016-09-09Android?獲取實(shí)時網(wǎng)速實(shí)現(xiàn)詳解
這篇文章主要為大家介紹了Android?獲取實(shí)時網(wǎng)速實(shí)現(xiàn)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11