利用Android6.0怎么實(shí)現(xiàn)一個(gè)指紋識別功能

本篇文章為大家展示了利用Android6.0怎么實(shí)現(xiàn)一個(gè)指紋識別功能,內(nèi)容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細(xì)介紹希望你能有所收獲。

成都創(chuàng)新互聯(lián)公司專注于云城網(wǎng)站建設(shè)服務(wù)及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗(yàn)。 熱誠為您提供云城營銷型網(wǎng)站建設(shè),云城網(wǎng)站制作、云城網(wǎng)頁設(shè)計(jì)、云城網(wǎng)站官網(wǎng)定制、微信平臺小程序開發(fā)服務(wù),打造云城網(wǎng)絡(luò)公司原創(chuàng)品牌,更為您提供云城網(wǎng)站排名全網(wǎng)營銷落地服務(wù)。

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

谷歌在android6.0及以上版本對指紋識別進(jìn)行了官方支持。當(dāng)時(shí)在FingerprintManager和FingerprintManagerCompat這兩個(gè)之間糾結(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í)間,提示嘗試次數(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)證的指紋成功時(shí)會回調(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()方法,這時(shí)候指紋傳感器并沒有關(guān)閉,谷歌原生系統(tǒng)給了我們5次重試機(jī)會,也就是說,連續(xù)調(diào)用了4次onAuthenticationFailed()方法后,第5次會調(diào)用onAuthenticateError(int errMsgId, CharSequence errString)方法,此時(shí)errMsgId==7。

2、每次重新授權(quán),哪怕不去校驗(yàn),取消時(shí)會走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)時(shí)加入CryptoObject 。crypto這是一個(gè)加密類的對象,指紋掃描器會使用這個(gè)對象來判斷認(rèn)證結(jié)果的合法性。這個(gè)對象可以是null,但是這樣的話,就意味著app無條件信任認(rèn)證的結(jié)果,這個(gè)過程可能被攻擊,數(shù)據(jù)可以被篡改,這是app在這種情況下必須承擔(dān)的風(fēng)險(xiǎn)。因此,建議這個(gè)參數(shù)不要置為null。

上述內(nèi)容就是利用Android6.0怎么實(shí)現(xiàn)一個(gè)指紋識別功能,你們學(xué)到知識或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識儲備,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。

網(wǎng)頁標(biāo)題:利用Android6.0怎么實(shí)現(xiàn)一個(gè)指紋識別功能
文章地址:http://bm7419.com/article14/jjsjge.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供關(guān)鍵詞優(yōu)化、App設(shè)計(jì)、網(wǎng)站收錄、電子商務(wù)、企業(yè)建站、面包屑導(dǎo)航

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站優(yōu)化排名