android 简单实现指纹识别功能

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_31796651/article/details/82887658

android6.0以后就提供了指纹识别功能,但是由于android手机被各大厂商弄的层次不齐,所以android的指纹识别使用的比较少,但是由于现在老的智能手机被淘汰的差不多了,新手机几乎无一不支持指纹解锁的,所以android也可以搞起指纹识别来了,使使用更方便。

指纹验证的思路大概是这样的:

  • 1.设备是否支持指纹识别
  • 2.设备是否处于安全保护中(有指纹识别的手机,在使用指纹识别的时候,还需要强制设置密码解锁,如果未设置的话是不许使用指纹识别的)
  • 3.设备是否已经注册过指纹(如果用户未使用过这个指纹技术,那么只能提示用户到系统设置里面去设置)

先看一张效果图:

废话不多说直接上代码吧:

要想查看google文档的请点这里

第一步:在AndroidManifest.xml中申明权限:

<uses-permission android:name="android.permission.USE_FINGERPRINT"/>

此权限不需要动态去申请,直接注册就可以;关于哪些权限需要去动态申请可以查看鸿洋大神的这篇博客:

http://blog.csdn.net/lmj623565791/article/details/50709663

第二步:获取指纹管理类:

  //这种是使用系统服务,但是必须要在sdk为23以上版本才行
//        FingerprintManager fingerprintManager = (FingerprintManager) getSystemService(Context.FINGERPRINT_SERVICE);
        //这种使用的是v4的兼容包,推荐使用这种
        managerCompat = FingerprintManagerCompat.from(MyApplication.appContext);
 public boolean checkIsFinger() {
        //判断当前手机版本
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
//            if (ActivityCompat.checkSelfPermission(MyApplication.appContext, Manifest.permission.USE_FINGERPRINT) != PackageManager.PERMISSION_GRANTED) {
//                ToastUtils.getInstance().showToast("没有指纹识别权限");
//                return false;
//            }
            Log.e(TAG, "有指纹权限");
            //判断硬件是否支持指纹识别
            if (!managerCompat.isHardwareDetected()) {
                ToastUtils.getInstance().showToast("没有指纹识别模块");
                return false;
            }
            Log.e(TAG, "有指纹模块");
            //判断 是否开启锁屏密码
            if (!keyguardManager.isKeyguardSecure()) {
                ToastUtils.getInstance().showToast("没有开启锁屏密码");
                return false;
            }
            //判断是否有指纹录入
            if (!managerCompat.hasEnrolledFingerprints()) {
                ToastUtils.getInstance().showToast("没有录入指纹");
                return false;
            }
            return true;
        } else {
            ToastUtils.getInstance().showToast("设备系统版本太低不支持指纹识别");
            return false;
        }
    }
 /**
     * 开始识别指纹
     *
     * @param listener
     */
    public void callFingerPrint(final FingerRecognitionCallBack listener) {
        authentFailedTimes = 0;
        if (listener != null)
            listener.onAuthenticationStart(); //开始指纹识别
        cancellationSignal = new CancellationSignal(); //必须重新实例化,否则cancel 过一次就不能再使用了
        managerCompat.authenticate(null, 0, cancellationSignal, new FingerprintManagerCompat.AuthenticationCallback() {
            // 验证出错回调 指纹传感器会关闭一段时间,在下次调用authenticate时,会出现禁用期(时间依厂商不同30,1分都有)
            @Override
            public void onAuthenticationError(int errMsgId, CharSequence errString) {
                if (listener != null)
                    listener.onAuthenticationError(errMsgId, errString);
            }

            // 验证失败  指纹验证失败后,指纹传感器不会立即关闭指纹验证,系统会提供5次重试的机会,即调用5次onAuthenticationFailed后,才会调用onAuthenticationError
            @Override
            public void onAuthenticationFailed() {
                authentFailedTimes++;
                if (listener != null)
                    listener.onAuthenticationFailed(authentFailedTimes);
            }

            @Override
            public void onAuthenticationHelp(int helpMsgId, CharSequence helpString) {//比如手指移动太快等情况
                if (listener != null)
                    listener.onAuthenticationHelp(helpMsgId, helpString);
            }

            // 当验证的指纹成功时会回调此函数,然后不再监听指纹sensor
            @Override
            public void onAuthenticationSucceeded(FingerprintManagerCompat.AuthenticationResult result) {
                if (listener != null)
                    listener.onAuthenticationSucceeded(result);
            }
        }, null);
    }
if (FingerUnlockUtil.init().checkIsFinger()) {
                    final AlertDialog dia = localBuilder.show();
                    FingerUnlockUtil.init().callFingerPrint(new FingerRecognitionCallBackHelper() {

                        @Override
                        public void onAuthenticationError(int errMsgId, CharSequence errString) {
                            dia.dismiss();
                            ToastUtils.getInstance().showToast("验证错误===" + String.valueOf(errString));
                        }

                        @Override
                        public void onAuthenticationFailed(int failedTimes) {
                            ToastUtils.getInstance().showToast("已经验证错误===" + failedTimes + "次");
                        }

                        @Override
                        public void onAuthenticationHelp(int helpMsgId, CharSequence helpString) {//可以不用实现
                            ToastUtils.getInstance().showToast("验证帮助===" + String.valueOf(helpString));
                        }

                        @Override
                        public void onAuthenticationSucceeded(FingerprintManagerCompat.AuthenticationResult result) {
                            dia.dismiss();
                            ToastUtils.getInstance().showToast("验证成功===");
                        }
                    });
                }

这里就结束了,是不是很简单呢,可以点击这里下载源码:demo源码

猜你喜欢

转载自blog.csdn.net/qq_31796651/article/details/82887658