Android 指紋識別
阿新 • • 發佈:2019-01-04
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"