先生,我正在尝试在我的应用程序中添加指纹身份验证。我有酷派笔记 3 精简版手机,支持指纹身份验证,它运行在安卓版本 5.1 棒棒糖。但问题是我如何验证其中的指纹。 我看过Android开发人员文档,他们说只有在棉花糖或更高版本上运行的设备上才有可能。 那么我如何在我的设备上执行此操作..
我在互联网上找到的代码是这样的:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
//Get an instance of KeyguardManager and FingerprintManager//
keyguardManager =
(KeyguardManager) getSystemService(KEYGUARD_SERVICE);
fingerprintManager =
(FingerprintManager) getSystemService(FINGERPRINT_SERVICE);
textView = (TextView) findViewById(R.id.textview);
//Check whether the device has a fingerprint sensor//
if (!fingerprintManager.isHardwareDetected()) {
// If a fingerprint sensor isn’t available, then inform the user that they’ll be unable to use your app’s fingerprint functionality//
textView.setText("Your device doesn't support fingerprint authentication");
}
//Check whether the user has granted your app the USE_FINGERPRINT permission//
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.USE_FINGERPRINT) != PackageManager.PERMISSION_GRANTED) {
// If your app doesn't have this permission, then display the following text//
textView.setText("Please enable the fingerprint permission");
}
//Check that the user has registered at least one fingerprint//
if (!fingerprintManager.hasEnrolledFingerprints()) {
// If the user hasn’t configured any fingerprints, then display the following message//
textView.setText("No fingerprint configured. Please register at least one fingerprint in your device's Settings");
}
//Check that the lockscreen is secured//
if (!keyguardManager.isKeyguardSecure()) {
// If the user hasn’t secured their lockscreen with a PIN password or pattern, then display the following text//
textView.setText("Please enable lockscreen security in your device's Settings");
} else {
try { generateKey();
} catch (FingerprintException e) {
e.printStackTrace();
}
if (initCipher()) {
//If the cipher is initialized successfully, then create a CryptoObject instance//
cryptoObject = new FingerprintManager.CryptoObject(cipher);
// Here, I’m referencing the FingerprintHandler class that we’ll create in the next section. This class will be responsible
// for starting the authentication process (via the startAuth method) and processing the authentication process events//
FingerprintHandler helper = new FingerprintHandler(this);
helper.startAuth(fingerprintManager, cryptoObject);
}
}
}
指纹扫描仪不是以下棉花糖的功能(现在在Android M上可用(。
所以每个公司如三星、摩托罗拉、HTC一样创建自己的API和SDK来访问指纹传感器。
这是堆栈溢出线程
或者,您可以使用FingerprintManagerCompat
类支持库 v4 来支持小于 23 的 minSdk。
查找有关开发人员文档的更多信息
GitHub 示例
根据@Michael评论
请注意,FingerprintManagerCompat
不允许您访问棉花糖之前设备上可能存在的任何指纹传感器。它只为您省去了自己检查 API 级别的麻烦。