Java 如何使用指纹扫描仪对用户进行身份验证
我一直在开发我的个人android应用程序来存储我的密码。(因为lastpass是为手机付费的)。我目前使用Java 如何使用指纹扫描仪对用户进行身份验证,java,android,authentication,fingerprint,Java,Android,Authentication,Fingerprint,我一直在开发我的个人android应用程序来存储我的密码。(因为lastpass是为手机付费的)。我目前使用简单密码验证,但我希望能够利用我的指纹扫描仪 在文件AndroidManifest.xml中: <uses-permission android:name="android.permission.USE_FINGERPRINT" /> 但是我如何使用手机所有者的指纹对其进行身份验证呢 更新: 我用了卢波米尔·巴别夫的答案,而且答案很完美。你填写了你在OnAuthSucceed
简单密码验证
,但我希望能够利用我的指纹
扫描仪
在文件AndroidManifest.xml
中:
<uses-permission android:name="android.permission.USE_FINGERPRINT" />
但是我如何使用手机所有者的指纹对其进行身份验证呢
更新:
我用了卢波米尔·巴别夫的答案,而且答案很完美。你填写了你在OnAuthSucceed上实现的两个方法,onAuthFailed无法处理授权是否成功,我还必须添加一些权限检查,因为Android studio让我这么做
public void startListening() {
if (isFingerScannerAvailableAndSet()) {
try {
if (ActivityCompat.checkSelfPermission(mContext.getApplicationContext(), Manifest.permission.USE_FINGERPRINT) != PackageManager.PERMISSION_GRANTED) {
}
mFingerprintManager.authenticate(null, mCancellationSignal, 0 /* flags */, mAuthenticationCallback, null);
} catch (Exception e) {
e.printStackTrace();
}
}
}
及
我为指纹事件创建自定义处理程序类:
import android.content.Context;
import android.hardware.fingerprint.FingerprintManager;
import android.os.Build;
import android.os.CancellationSignal;
public class FingerprintHandler {
private Context mContext;
private FingerprintManager mFingerprintManager = null;
private CancellationSignal mCancellationSignal;
private FingerprintManager.AuthenticationCallback mAuthenticationCallback;
private OnAuthenticationSucceededListener mSucceedListener;
private OnAuthenticationErrorListener mFailedListener;
public interface OnAuthenticationSucceededListener {
void onAuthSucceeded();
}
public interface OnAuthenticationErrorListener {
void onAuthFailed();
}
public void setOnAuthenticationSucceededListener (OnAuthenticationSucceededListener listener){
mSucceedListener = listener;
}
public void setOnAuthenticationFailedListener(OnAuthenticationErrorListener listener) {
mFailedListener = listener;
}
public FingerprintHandler(Context context){
mContext = context;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
mFingerprintManager = context.getSystemService(FingerprintManager.class);
mCancellationSignal = new CancellationSignal();
mAuthenticationCallback = new FingerprintManager.AuthenticationCallback() {
@Override
public void onAuthenticationError(int errorCode, CharSequence errString) {
super.onAuthenticationError(errorCode, errString);
}
@Override
public void onAuthenticationHelp(int helpCode, CharSequence helpString) {
super.onAuthenticationHelp(helpCode, helpString);
}
@Override
public void onAuthenticationSucceeded(FingerprintManager.AuthenticationResult result) {
super.onAuthenticationSucceeded(result);
if( mSucceedListener != null )
mSucceedListener.onAuthSucceeded();
}
@Override
public void onAuthenticationFailed() {
super.onAuthenticationFailed();
if (mFailedListener != null)
mFailedListener.onAuthFailed();
}
};
}
}
public void startListening(){
if (isFingerScannerAvailableAndSet() ) {
try{
mFingerprintManager.authenticate(null, mCancellationSignal, 0 /* flags */, mAuthenticationCallback, null);
} catch (Exception e){
e.printStackTrace();
}
}
}
public void stopListening(){
if ( isFingerScannerAvailableAndSet() ) {
try {
mCancellationSignal.cancel();
mCancellationSignal = null;
} catch (Exception e){
e.printStackTrace();
}
}
}
public boolean isFingerScannerAvailableAndSet(){
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M)
return false;
if( mFingerprintManager == null )
return false;
if( !mFingerprintManager.isHardwareDetected() )
return false;
if( !mFingerprintManager.hasEnrolledFingerprints())
return false;
return true;
}
}
在初始化之后,onCreate方法中的指纹处理程序:
mFingerprintHandler = new FingerprintHandler(this);
mFingerprintHandler.setOnAuthenticationSucceededListener(this);
mFingerprintHandler.setOnAuthenticationFailedListener(this);
您可以通过以下方法检查指纹是否可用,并在活动中设置:
if( mFingerprintHandler.isFingerScannerAvailableAndSet() ){
// show image or text or do something
}
您可以通过以下方法处理指纹响应:
@Override
public void onAuthSucceeded() {
//fingerprint auth succeded go to next activity (or do something)
}
@Override
public void onAuthFailed() {
//fingerpring auth failed, show error toast (or do something)
}
你准备好了!:)
不要忘记在onPause和onResume方法中停止并开始监听指纹:
@Override
public void onResume() {
super.onResume();
mFingerprintHandler.startListening();
}
@Override
public void onPause() {
super.onPause();
mFingerprintHandler.stopListening();
}
快乐编码:)你可以用这个。它支持所有锁定机制(PIN、图案、密码、指纹扫描仪)
<代码> LaSTPASS被支付< /代码> -好吧,尽管推理完全由你决定,我会说你根本不认为你的时间有价值。“最后一个通行证一年只需12美元。如果你只是为了取代最后一个通行证,那就看看keepass吧。@MarcinOrlowski我知道你在说什么,但既然我正在学习android rn,这对我来说是一个发现moreHoly的大好机会。”。。你是我的英雄,@lubomirbebev一切都像微风一样。我唯一要做的就是添加一些权限检查,但仅此而已。。谢谢,从现在起,我们将在每个应用程序中使用此功能
if( mFingerprintHandler.isFingerScannerAvailableAndSet() ){
// show image or text or do something
}
@Override
public void onAuthSucceeded() {
//fingerprint auth succeded go to next activity (or do something)
}
@Override
public void onAuthFailed() {
//fingerpring auth failed, show error toast (or do something)
}
@Override
public void onResume() {
super.onResume();
mFingerprintHandler.startListening();
}
@Override
public void onPause() {
super.onPause();
mFingerprintHandler.stopListening();
}
Intent credentialsIntent = null;
KeyguardManager keyguardManager = (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP)
credentialsIntent = keyguardManager.createConfirmDeviceCredentialIntent("title", "desc"), context.getString(R.string.verification_desc));
//If phone lock is set, launch the unlock screen
if (credentialsIntent != null) {
((Activity) context).startActivityForResult(credentialsIntent, CREDENTIALS_RESULT);
}
//Phone is not locked
else {
doTheWork();
}
@Override
public void onActivityResult(int requestCode) {
if (requestCode == CREDENTIALS_RESULT) {
doTheWork;
}
else
Log.e("TA", "Error");
}