Android6.0 Fingerprint Features(指纹识别)

来源:互联网 发布:pe备份数据 编辑:程序博客网 时间:2024/04/27 17:23

关于指纹识别需要了解的:

  • 要开启指纹识别,必须开启密码锁屏(Pin,Password,Pattern等),如果锁屏密码被清除,或者设置为滑动,之前录入的指纹会被删除。

  • 识别错误回调onAuthenticationFailed,错误六次之后,回调onAuthenticationError,onAuthenticationError被回调后,后续不会再有任何回调(直到取消本次指纹输入,重新调用authenticate后才可以)

  • onAuthenticationError发生后,即使退出后重新调用authenticate方法,仍然马上就会回调onAuthenticationError方法,而不是等待输入指纹,只有间隔一定时间之后,重新调用authenticate,才能开启指纹传感器等待输入指纹


简介

最新的Android6.0系统中的指纹识别功能是一大亮点,开发者可以考虑用这个功能来简化一些鉴权方面的UI交互(避免频繁输入password,也避免用户记太多的password)。新的SDK中指纹识别api被分为两类,一类为Fingerprint API,另一类为Confirm Credential, 本文分别介绍这两类api各自特点相互的区别,以及使用方法。


Fingerprint API & Confirm Credential 区别

  • Fingerprint API一个纯指纹相关的API,就是为了提供指纹鉴别的功能,调用authenticate (callback)后指纹传感器开始等待指纹录入,用户录入指纹后通过callback回调识别是否成功(实际的authenticate方法还有一些其他的参数),这个API没有任何系统UI,所以开发者可以设计指纹识别流程的UI,只要确保正确的调用authenticate(callback)方法,正确的处理其中的callback即可。

  • Confirm Credential 核心是提供了一个系统的解锁界面Activity,使用的时候首先用mKeyguardManager.createConfirmDeviceCredentialIntent(null, null);方法获取到一个Intent,然后 startActivityForResult(intent, REQUEST_CODE_CONFIRM_DEVICE_CREDENTIALS)的方式拉起Activity,解锁是否成功则在onActivityResult回调中体现。这个系统的解锁界面类似锁屏界面,可以用锁屏密码或者指纹解锁。
    注意:此处的解锁界面具体解锁方式和手机锁屏设置一致,例如锁屏设置的是图案解锁那么这里就是图案解锁,锁屏设置的是密码或者PIN码解锁,那么这里也是密码或者PIN码解锁。


Fingerprint API的使用:

**首先记得在manifest中添加<uses-permission
android:name="android.permission.USE_FINGERPRINT" />
权限**
使用前先判断是否硬件支持,是否有指纹等:

if (!mFingerprintManager.isHardwareDetected()) {               // 判断硬件是否支持                Toast.makeText(this,                        "Fingerprint hardward unavailable",                        Toast.LENGTH_LONG).show();                return;            }if (!mKeyguardManager.isKeyguardSecure()) {            // 判断用户是已经开启锁屏密码            Toast.makeText(this,                    "Secure lock screen hasn't set up.\n"                            + "Go to 'Settings -> Security -> Screenlock' to set up a lock screen",                    Toast.LENGTH_LONG).show();            return;        }if (!mFingerprintManager.hasEnrolledFingerprints()) {                // 判断是否有已经录入的指纹                Toast.makeText(this,                        "Go to 'Settings -> Security -> Fingerprint' and register at least one fingerprint",                        Toast.LENGTH_LONG).show();                return;            }

使用startListening()打开指纹传感器:

boolean mSelfCancelled = false;    public boolean isFingerprintAuthAvailable() {        return mFingerprintManager.isHardwareDetected()                && mFingerprintManager.hasEnrolledFingerprints();    }    public void startListening(FingerprintManager.CryptoObject cryptoObject) {        if (!isFingerprintAuthAvailable()) {            return;        }        mSelfCancelled = false;        CancellationSignal mCancellationSignal = new CancellationSignal();        mFingerprintManager                .authenticate(null, mCancellationSignal, 0, callback , null);    }    public void stopListening() {        if (mCancellationSignal != null) {            mSelfCancelled = true;            mCancellationSignal.cancel();            mCancellationSignal = null;        }    }

使用callback定义回调:(callback为上面authenticate()方法中的一个参数)

FingerprintManager.AuthenticationCallback callback = new FingerprintManager.AuthenticationCallback() {        @Override        public void onAuthenticationError(int errMsgId, CharSequence errString) {            if (!mSelfCancelled) {                showError(errString);            }        }        @Override        public void onAuthenticationHelp(int helpMsgId, CharSequence helpString) {            showError(helpString);        }        @Override        public void onAuthenticationFailed() {            showError(getActivity().getResources().getString(                    R.string.fingerprint_not_recognized));        }        @Override        public void onAuthenticationSucceeded(FingerprintManager.AuthenticationResult result) {            showSuccess();        }

Confirm Credential 的使用

由于认证页面为系统的一个Activity所以,app本身 不需要 申明指纹权限<uses-permission
android:name="android.permission.USE_FINGERPRINT" />

使用前先判断是否已经开启锁屏密码:(如果没有开启,后面的createConfirmDeviceCredentialIntent()方法会反回一个null 的intent)

if (!mKeyguardManager.isKeyguardSecure()) {            // 判断用户是已经开启锁屏密码            Toast.makeText(this,                    "Secure lock screen hasn't set up.\n"                            + "Go to 'Settings -> Security -> Screenlock' to set up a lock screen",                    Toast.LENGTH_LONG).show();            return;        }

在Activity中唤起系统认证页面:

    private void showAuthenticationScreen() {        // createConfirmDeviceCredentialIntent(title,description)中可以自定义解锁页面的title和description:        Intent intent = mKeyguardManager.createConfirmDeviceCredentialIntent(null, null);        if (intent != null) {            startActivityForResult(intent, REQUEST_CODE_CONFIRM_DEVICE_CREDENTIALS);        }    }

在onActivityResult()中处理回调:

@Override    protected void onActivityResult(int requestCode, int resultCode, Intent data) {        if (requestCode == REQUEST_CODE_CONFIRM_DEVICE_CREDENTIALS) {            // Challenge completed, proceed with using cipher            if (resultCode == RESULT_OK) {                showSuccess();            } else {                showError();            }        }    }
1 0
原创粉丝点击