Android 指纹识别

1. Android 6.0指纹验证

1). 获取指纹管理者
  /**
   * 指纹管理者
   */
  private var mFingerprintManager: FingerprintManager? = null
  /**
   * 获取指纹管理者
   */
  private fun getFingerprintManager(context: Context): FingerprintManager? {
    if (null == mFingerprintManager) {
      mFingerprintManager = context.getSystemService(FingerprintManager::class.java)
    }
    return mFingerprintManager
  }
2). 取消指纹对象
  /**
   * 控制取消对象
   */
  private var mCancellationSignal: CancellationSignal? = null

// ===========================================================================
// 方法内部代码:
    // 初始化
    if (null != this.mCancellationSignal) {
      this.mCancellationSignal = CancellationSignal()
    }
3). 初始化加密工具类
val cryptoObjectHelper = CryptoObjectHelper()
4). 指纹验证回调
  /**
   * 指纹验证回调
   */
  private val mFmAuthCallback: FingerprintManager.AuthenticationCallback = FingerprintManagerCallbackImpl()

  /**
   * 指纹验证回调
   * 内部类
   */
  inner class FingerprintManagerCallbackImpl : FingerprintManager.AuthenticationCallback() {
    override fun onAuthenticationError(errMsgId: Int, errString: CharSequence?) {
      super.onAuthenticationError(errMsgId, errString)
//      Log.d("TAG","onAuthenticationError() called with: errMsgId = [$errMsgId], errString = [$errString]")
      mDialog?.setState(BiometricPromptDialog.STATE_ERROR)
      mManagerIdentifyCallback?.onError(errMsgId, errString.toString())
    }
    
    override fun onAuthenticationFailed() {
      super.onAuthenticationFailed()
//      Log.d("TAG","onAuthenticationFailed() called")
      mDialog?.setState(BiometricPromptDialog.STATE_FAILED)
      mManagerIdentifyCallback?.onFailed()
    }
    
    override fun onAuthenticationHelp(helpMsgId: Int, helpString: CharSequence?) {
      super.onAuthenticationHelp(helpMsgId, helpString)
//      Log.d("TAG","onAuthenticationHelp() called with: helpMsgId = [$helpMsgId], helpString = [$helpString]")
      mDialog?.setState(BiometricPromptDialog.STATE_FAILED)
      mManagerIdentifyCallback?.onFailed()
    }
    
    override fun onAuthenticationSucceeded(result: FingerprintManager.AuthenticationResult?) {
      super.onAuthenticationSucceeded(result)
//      Log.d("TAG","onAuthenticationSucceeded: $result")
      mDialog?.setState(BiometricPromptDialog.STATE_SUCCEED)
      mManagerIdentifyCallback?.onSucceed()
    }
  }
5). 指纹验证
mFingerprintManager?.authenticate(cryptoObjectHelper.buildCryptoObject(), mCancellationSignal, 0, mFmAuthCallback, null)
6). 判断是否有指纹功能
  /**
   * 判断是否有指纹功能
   * @return true: 有指纹功能; false: 没有指纹功能
   */
  fun isHardwareDetected(): Boolean {
    return mFingerprintManager?.isHardwareDetected ?: false
  }
7). 判断是否有指纹录入
  /**
   * 判断是否有指纹录入
   * @return true: 有指纹录入; false: 没有指纹录入
   */
  fun hasEnrolledFingerprints(): Boolean {
    return mFingerprintManager?.hasEnrolledFingerprints() ?: false
  }
8). 注意

Android 6.0 需要用户自定义指纹验证对话框

2. Android 9.0指纹验证

1). 初始化生物识别对象
  /**
   * 生物识别对象
   */
  private var mBiometricPrompt: BiometricPrompt? = null

  mBiometricPrompt = BiometricPrompt
            .Builder(activity)
            .setTitle(activity.resources.getString(R.string.biometric_dialog_title))
            .setDescription(activity.resources.getString(R.string.biometric_dialog_subtitle))
            .setSubtitle("")
            .setNegativeButton(activity.resources.getString(R.string.biometric_dialog_use_password),
                    activity.mainExecutor, DialogInterface.OnClickListener { _, _ ->
              mManagerIdentifyCallback?.onUsePassword()
              mCancellationSignal?.cancel()
            })
            .build()
2). 生成签名信息
  /**
   * 签名对象
   */
  private var mSignature: Signature? = null
  // 初始化签名
  mSignature = initSignature(KEY_NAME)
3). 初始化控制取消对象
/**
 * 控制取消对象
 */
private var mCancellationSignal: CancellationSignal? = null
if (null == this.mCancellationSignal) {
      this.mCancellationSignal = CancellationSignal()
}
4). 生物验证回调
  /**
   * 生物验证回调
   */
  @RequiresApi(Build.VERSION_CODES.P)
  private inner class BiometricPromptCallbackImpl : BiometricPrompt.AuthenticationCallback() {
    override fun onAuthenticationError(errorCode: Int, errString: CharSequence?) {
      super.onAuthenticationError(errorCode, errString)
      mCancellationSignal?.cancel()
    }
  
    override fun onAuthenticationSucceeded(result: BiometricPrompt.AuthenticationResult?) {
      super.onAuthenticationSucceeded(result)
      mManagerIdentifyCallback?.onSucceed()
      mCancellationSignal?.cancel()
    }
  }
5). 验证
mBiometricPrompt?.authenticate(BiometricPrompt.CryptoObject(mSignature!!), mCancellationSignal!!, this.mActivity.mainExecutor, BiometricPromptCallbackImpl())

代码下载

依赖

  implementation "com.mazaiting:sp:1.0.0"
  implementation "com.mazaiting:biometric:1.0.0"
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 用两张图告诉你,为什么你的 App 会卡顿? - Android - 掘金 Cover 有什么料? 从这篇文章中你...
    hw1212阅读 14,586评论 2 59
  • 指纹识别大致应用在几种场景1,系统解锁2,应用锁3,支付认证4,普通的登录认证 指纹识别需要手机硬件支持才能使用。...
    ReleaseYH阅读 10,287评论 0 6
  • 指纹识别相关api FingerprintManagaerCompat 指纹识别的核心包装类 初始化方法 用于判断...
    好多个胖子阅读 6,452评论 1 10
  • 后来,在support v4库中添加了FingerprintManagerCompat类,我看了他的源码,其实就是...
    UP7CR阅读 46,617评论 41 190
  • 2016年年中的时候因为个人的一些原因,在家休息了几个月。天气好就会拿着自己的佳能70d出去拍照,也没有一定要拍什...
    真白酱阅读 1,603评论 0 1

友情链接更多精彩内容