如何使用指纹扫描仪对用户进行身份验证

How to use fingerprint scanner to authenticate users

我一直在开发我的个人 android 应用程序来存储我的密码。 (因为 lastpass 是为手机付费的)。我目前使用 simple password authentication,但我希望能够利用我的 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 phone 所有者如何使用他的 fingerprints

更新:

我使用了 Lubomir Babev 的答案并且非常完美。您填写您实现的两个方法 onAuthSucceded,onAuthFailed 以处理授权是否成功,我还必须添加一些权限检查,因为 Android 工作室让我这样做

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();
        }
    }
}

我为指纹事件创建了自定义处理程序 class:

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;
    }
}

然后在你的 activity 中执行

FingerprintHandler.OnAuthenticationSucceededListener, FingerprintHandler.OnAuthenticationErrorListener

创建指纹参数:

private FingerprintHandler mFingerprintHandler;

之后在 onCreate 方法中初始化此指纹处理程序:

mFingerprintHandler = new FingerprintHandler(this);
mFingerprintHandler.setOnAuthenticationSucceededListener(this);
mFingerprintHandler.setOnAuthenticationFailedListener(this);

您可以检查指纹是否可用并在您的 activity 中设置:

    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、模式、密码、指纹扫描仪)。

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");
}