如何在 under marshmallow 设备上添加指纹认证
How to add fingerprint authentication on under marshmallow devices
先生,我正在尝试在我的应用程序中添加指纹验证。我有支持指纹认证的 Coolpad note 3 lite 手机,它是 运行 on Android version 5.1 Lollipop。但问题是我如何验证其中的指纹。
我看过 Android 开发人员文档,他们说只有在 Marshmallow 或更高版本的设备 运行 上才有可能。
那么我怎样才能在我的设备上做到这一点..
我在网上找到的代码是这样的:
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);
}
}
}
指纹扫描仪不是以下 Marshmallow 中的功能(现在在 Android M 上可用)。
因此三星、摩托罗拉、HTC 等每家公司都创建了自己的 API 和 SDK 来访问指纹传感器。
或者你可以使用Support Library v4的FingerprintManagerCompat
class来支持23以下的minSdk。
上查找更多信息
根据@Michael 评论
请注意,FingerprintManagerCompat
不允许您访问 Marshmallow 之前的设备上可能存在的任何指纹传感器。它只是省去了您自己检查 API 级别的麻烦。
先生,我正在尝试在我的应用程序中添加指纹验证。我有支持指纹认证的 Coolpad note 3 lite 手机,它是 运行 on Android version 5.1 Lollipop。但问题是我如何验证其中的指纹。 我看过 Android 开发人员文档,他们说只有在 Marshmallow 或更高版本的设备 运行 上才有可能。 那么我怎样才能在我的设备上做到这一点..
我在网上找到的代码是这样的:
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);
}
}
}
指纹扫描仪不是以下 Marshmallow 中的功能(现在在 Android M 上可用)。
因此三星、摩托罗拉、HTC 等每家公司都创建了自己的 API 和 SDK 来访问指纹传感器。
或者你可以使用Support Library v4的FingerprintManagerCompat
class来支持23以下的minSdk。
根据@Michael 评论
请注意,FingerprintManagerCompat
不允许您访问 Marshmallow 之前的设备上可能存在的任何指纹传感器。它只是省去了您自己检查 API 级别的麻烦。