欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android6.0指紋識別開發(fā)實例詳解

 更新時間:2017年04月18日 08:40:26   投稿:lqh  
這篇文章主要介紹了Android6.0指紋識別開發(fā)實例詳解的相關(guān)資料,需要的朋友可以參考下

Android6.0指紋識別開發(fā)實例詳解

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

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

實例代碼:

 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);
    }

    //指紋驗證失敗走此方法,例如小米前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);

    }

    //當驗證的指紋成功時會回調(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;

 }

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

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

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

3、當系統(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無論手機是否有指紋識別模塊,均認為沒有指紋識別,可以用FingerprintManager來做。

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

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

相關(guān)文章

最新評論