问题描述
我一直在开发我的个人android应用程序来存储我的密码. (由于为移动支付了lastpass的费用).我目前使用simple password authentication
,但是我希望能够利用我的fingerprint
scanner
.
I have been developing my personal android app to store my passwords. (Since lastpass is paid for mobile). I currently use simple password authentication
, but i would love to be able to take advantage of my fingerprint
scanner
.
AndroidManifest.xml:
<uses-permission android:name="android.permission.USE_FINGERPRINT" />
MainActivity.java:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
//Fingerprint API only available on from Android 6.0 (M)
FingerprintManager fingerprintManager = (FingerprintManager) context.getSystemService(Context.FINGERPRINT_SERVICE);
if (!fingerprintManager.isHardwareDetected()) {
// Device doesn't support fingerprint authentication
} else if (!fingerprintManager.hasEnrolledFingerprints()) {
// User hasn't enrolled any fingerprints to authenticate with
} else {
// Everything is ready for fingerprint authentication
}
}
但是我实际上如何authenticate
电话所有者使用他的fingerprints
?
but how do i actually authenticate
phone owner using his fingerprints
?
更新:
我使用了卢博米尔·贝贝夫(Lubomir Babev)的答案及其完美之处.您填写了实现onAuthSucceded和onAuthFailed的两种方法,以实现authorizartion成功,并且我还必须添加一些权限检查,因为Android Studio让我这么做了
I used Lubomir Babev's answer and its perfect. You fill the two methods that you implement onAuthSucceded, onAuthFailed to handle if authorizartion was successful and i also had to add some permission checks, because Android studio made me do it
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();
}
}
}
和
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();
}
}
}
推荐答案
我为指纹事件创建了一个自定义处理程序类:
I create a custom handler class for fingerprint event :
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;
}
}
然后在您的活动工具中
FingerprintHandler.OnAuthenticationSucceededListener, FingerprintHandler.OnAuthenticationErrorListener
创建指纹参数:
private FingerprintHandler mFingerprintHandler;
之后,在onCreate方法中初始化此指纹处理程序:
After that init this fingerprint handler in onCreate method :
mFingerprintHandler = new FingerprintHandler(this);
mFingerprintHandler.setOnAuthenticationSucceededListener(this);
mFingerprintHandler.setOnAuthenticationFailedListener(this);
您可以通过以下方法检查指纹是否可用并在您的活动中进行设置:
You can check if fingerprint is avaivable and set in your activity with this :
if( mFingerprintHandler.isFingerScannerAvailableAndSet() ){
// show image or text or do something
}
您可以通过已实现的方法来处理指纹响应:
You can handle your fingerprint response in implemented methods :
@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方法监听指纹:
And you are ready ! :)Don't forget to stop and start listening the fingerprint in onPause and onResume methods :
@Override
public void onResume() {
super.onResume();
mFingerprintHandler.startListening();
}
@Override
public void onPause() {
super.onPause();
mFingerprintHandler.stopListening();
}
幸福的礼物:)))
这篇关于如何使用指纹扫描仪对用户进行身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!