Android指纹识别

Android指纹识别


一、在manifest文件中添加权限

<!--指纹识别权限-->
<uses-permission android:name="android.permission.USE_FINGERPRINT" />

二、具体代码实现

1.判断是否有指纹识别功能

/**
 * 判断是否具有指纹识别功能和是否已经设置指纹
 * */
@SuppressLint("NewApi")
public boolean isFingerprintAuthAvailable() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {//判断当前手机版本
        KeyguardManager mKeyguardManager = (KeyguardManager) getSystemService(Activity.KEYGUARD_SERVICE);//获得密码管理器
        if (!mKeyguardManager.isKeyguardSecure()) {//判断是否具有锁屏密码
            return false;
        }
        if (checkSelfPermission(Manifest.permission.USE_FINGERPRINT) == PackageManager.PERMISSION_GRANTED) {//判断APP是否具有指纹识别权限
            FingerprintManager manager = (FingerprintManager) getSystemService(Activity.FINGERPRINT_SERVICE); //获得指纹识别管理器对象
            CancellationSignal mCancellationSignal = new CancellationSignal();
            //判断是否具有指纹识别的硬件设施和是否已经录好指纹
            return manager.isHardwareDetected() && manager.hasEnrolledFingerprints();
        }else{
            return false;
        }
    }else{
        return false;
    }
}

2.开始指纹校验

/**
 * 开始指纹校验
 */
@SuppressLint("NewApi")
private void startFingerPrintListener() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        if (checkSelfPermission(Manifest.permission.USE_FINGERPRINT) == PackageManager.PERMISSION_GRANTED) {
            FingerprintManager manager = (FingerprintManager) getSystemService(Activity.FINGERPRINT_SERVICE);
            CancellationSignal mCancellationSignal = new CancellationSignal();
            //指纹检验
             manager.authenticate(null, mCancellationSignal, 0, new MyAuthenticationCallback(), null);
        }
    }
}

主要方法参数说明

public void authenticate(FingerprintManager.CryptoObject crypto,
    CancellationSignal cancel, 
    int flags,  
    FingerprintManager.AuthenticationCallback callback, Handler handler) {
    throw new RuntimeException("Stub!");
}

crypto:代表Android6.0中crypto objects的wrapper class,可以通过该对象使authenticate过程更加安全,也可以不使用,这里我们将其设为null;

cancel:用来取消anthenticate(),我们new出一个对象传入就可以;

flags:是标志位,设置为0;

callback为指纹识别回调,包含指纹识别的核心方法:

  • onAuthenticationError()是指纹匹配连续失败后的回调(几十秒后才能继续匹配),此时mCancellationSignal处于isCanceled()状态;
  • onAuthenticationSucceeded()是指纹匹配成功的回调,此时mCancellationSignal处于isCanceled()状态;
  • onAuthenticationFailed()是指纹匹配失败时的回调,此时mCancellationSignal处于!isCanceled()状态,可直接再次进行指纹识别;
  • onAuthenticationHelp()是失败提示回调,,此时mCancellationSignal处于!isCanceled()状态,可直接再次进行指纹识别;

3.指纹识别回调

@RequiresApi(api = Build.VERSION_CODES.M)
public class MyAuthenticationCallback extends FingerprintManager.AuthenticationCallback{
    private static final String TAG = "AuthenticationCallback";

    @Override
    public void onAuthenticationSucceeded(FingerprintManager.AuthenticationResult result) {
        super.onAuthenticationSucceeded(result);
        Log.d(TAG, "onAuthenticationSucceeded: " + "校验成功");
    }

    @Override
    public void onAuthenticationFailed() {
        super.onAuthenticationFailed();
        Log.d(TAG, "onAuthenticationFailed: " + "校验失败");
    }

    @Override
    public void onAuthenticationHelp(int helpCode, CharSequence helpString) {
        super.onAuthenticationHelp(helpCode, helpString);
        Log.d(TAG, "onAuthenticationHelp: " + helpString);
    }

    @Override
    public void onAuthenticationError(int errorCode, CharSequence errString) {
        super.onAuthenticationError(errorCode, errString);
        Log.d(TAG, "onAuthenticationError: " + errString);
    }
}

4.调用指纹识别功能

/**
 * 检验指纹功能
 */
private void fingerPrintCheck() {
    // 判断是否具有指纹识别功能
    if (!isFingerprintAuthAvailable()) {
        return;
    }else{
        startFingerPrintListener();
    }
}
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值