国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android6.0指紋識別開發實例詳解

2019-12-12 03:04:36
字體:
來源:轉載
供稿:網友

Android6.0指紋識別開發實例詳解

最近在做android指紋相關的功能,谷歌在android6.0及以上版本對指紋識別進行了官方支持。當時在FingerprintManager和FingerprintManagerCompat這兩個之間糾結,其中使用FingerprintManager要引入com.android.support:appcompat-v7包,考慮到包的大小,決定使用v4兼容包FingerprintManagerCompat來實現。

主要實現的工具類FingerprintUtil:驗證手機是否支持指紋識別方法callFingerPrintVerify(),主要驗證手機硬件是否支持(6.0及以上),有沒有錄入指紋,然后有沒有開啟鎖屏密碼,開始驗證對于識別成功,失敗可以進行相應的回調處理。

實例代碼:

 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,會停止響應一段時間,提示嘗試次數過多,請稍后再試。    @Override    public void onAuthenticationError(int errMsgId, CharSequence errString) {     if (listener != null)      listener.onAuthenticateError(errMsgId, errString);    }    //指紋驗證失敗走此方法,例如小米前4次驗證失敗走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);    }    //當驗證的指紋成功時會回調此函數,然后不再監聽指紋sensor    @Override    public void onAuthenticationSucceeded(FingerprintManagerCompat.AuthenticationResult result) {     if (listener != null)      listener.onAuthenticateSucceeded(result);    }   }, null);  } catch (Exception e) {   e.printStackTrace();  } } /**  * 是否錄入指紋,有些設備上即使錄入了指紋,但是沒有開啟鎖屏密碼的話此方法還是返回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;  } } /**  * 指紋識別回調接口  */ 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; }

參考了一些資料,做了一些驗證,得到一些結論:

1、當指紋識別失敗后,會調用onAuthenticationFailed()方法,這時候指紋傳感器并沒有關閉,谷歌原生系統給了我們5次重試機會,也就是說,連續調用了4次onAuthenticationFailed()方法后,第5次會調用onAuthenticateError(int errMsgId, CharSequence errString)方法,此時errMsgId==7。

2、每次重新授權,哪怕不去校驗,取消時會走onAuthenticateError(int errMsgId, CharSequence errString) 方法,其中errMsgId==5,

3、當系統調用了onAuthenticationError()和onAuthenticationSucceeded()后,傳感器會關閉,只有我們重新授權,再次調用authenticate()方法后才能繼續使用指紋識別功能。

4、兼容android6.0以下系統的話,不要使用FingerprintManagerCompat, 低于M的系統版本,FingerprintManagerCompat無論手機是否有指紋識別模塊,均認為沒有指紋識別,可以用FingerprintManager來做。

5、考慮到安全因素,最好authenticate(CryptoObject crypto, CancellationSignal cancel, int flags, AuthenticationCallback callback, Handler handler)時加入CryptoObject 。crypto這是一個加密類的對象,指紋掃描器會使用這個對象來判斷認證結果的合法性。這個對象可以是null,但是這樣的話,就意味著app無條件信任認證的結果,這個過程可能被攻擊,數據可以被篡改,這是app在這種情況下必須承擔的風險。因此,建議這個參數不要置為null。這個類的實例化有點麻煩,主要使用javax的security接口實現。

 感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 泰州市| 建始县| 嘉义县| 丰县| 获嘉县| 邢台市| 横峰县| 凭祥市| 富裕县| 桦川县| 博乐市| 尚志市| 富蕴县| 阿坝| 浮山县| 昌黎县| 方城县| 安徽省| 宁阳县| 永丰县| 临朐县| 大宁县| 浙江省| 桂平市| 六枝特区| 南丹县| 多伦县| 钟山县| 五台县| 大厂| 义马市| 海安县| 哈尔滨市| 平舆县| 冷水江市| 罗山县| 泗洪县| 祁门县| 易门县| 永年县| 东至县|