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"
网友评论