最近在做项目的时候遇到了添加打开app图像解锁的功能,自己嘴欠说现在都用指纹功能,自己给自己挖了一个坑,真是没谁了
从网上看了一些资料,但是给我demo考虑的不是很多,设备支不支持都没考虑,如果支持的话是否添加过指纹也不知道,其实方法都很简单。
废话不多说,贴上工具类和使用方法
package com.tsm.test;import androID.annotation.TargetAPI;import androID.app.Activity;import androID.app.KeyguardManager;import androID.content.Context;import androID.content.Intent;import androID.os.Build;import androID.support.v4.harDWare.fingerprint.FingerprintManagerCompat;import androID.support.v4.os.CancellationSignal;/** * Created by tsm on 2017/3/20. * <p/> * 指纹识别功能 * * 如果创建了该类的实例,必须调用 stopsFingerPrintListen 方法 * * 添加权限 * <uses-permission androID:name="androID.permission.USE_FINGERPRINT" /> * */public class FingerPrintUiHelper extends FingerprintManagerCompat.AuthenticationCallback { private final FingerPrintCallBack callback; private CancellationSignal signal; private FingerprintManagerCompat fingerprintManager; /** * 如果失败次数过多,调用系统的startActivityForResult * 这个是code */ public static final int REQUEST_CODE_CONFIRM_DEVICE_CREDENTIALS = 10; /** * 用于提示用户还可以尝试几次,比较友好 */ private int count; /** * 控制是否开启过指纹功能 */ public boolean isstartFinger; /** * 初始化指纹功能 * @param activity * @param callback */ public FingerPrintUiHelper(Activity activity,FingerPrintCallBack callback) { this.callback = callback; signal = new CancellationSignal(); fingerprintManager = FingerprintManagerCompat.from(activity); isstartFinger = false; if (!fingerprintManager.isHarDWareDetected()) { if (callback != null) callback.doNotSupportFinger(); return; } if (!fingerprintManager.hasEnrolledFingerprints()) { if (callback != null) callback.FingerClosed(); } } /** * 开始扫描指纹 */ public voID startFingerPrintListen() { count = 5; isstartFinger = true; fingerprintManager.authenticate(null,signal,this,null); } /** * 初始化未必调用 startFingerPrintListen * 所以添加变量控制 */ public voID stopsFingerPrintListen() { if (isstartFinger) { if (signal != null && !signal.isCanceled()) { signal.cancel(); } } } /** * 识别成功 * @param result */ @OverrIDe public voID onAuthenticationSucceeded(FingerprintManagerCompat.AuthenticationResult result) { if (callback != null) callback.onAuthenticationSucceeded(); } /** * 识别失败 */ @OverrIDe public voID onAuthenticationFailed() { count--; if (count > 0) { if (callback != null) callback.onAuthenticationFailed(count); return; } } /** * 有错误 * @param errMsgid * @param errString */ @OverrIDe public voID onAuthenticationError(int errMsgid,CharSequence errString) { if (errMsgid == 5) { if (callback != null) callback.FingerClosed(); return; } if (errMsgid == 7) { if (callback != null) callback.onAuthenticationError(); return; } } /** * 多次调用指纹识别失败后,调用此方法 * * @param activity */ @TargetAPI(Build.VERSION_CODES.LolliPOP) public static voID jumpToGesturePassCheck(Activity activity) { KeyguardManager keyguardManager = (KeyguardManager) activity.getSystemService(Context.KEyguard_SERVICE); Intent intent = keyguardManager.createConfirmDeviceCredentialintent("finger","测试指纹识别"); activity.startActivityForResult(intent,REQUEST_CODE_CONFIRM_DEVICE_CREDENTIALS); } interface FingerPrintCallBack { /** * 识别成功 */ voID onAuthenticationSucceeded(); /** * 识别失败 * * @param count 还可以尝试的次数 * @param count */ voID onAuthenticationFailed(int count); /** * 失败次数过多 */ voID onAuthenticationError(); /** * 未开启指纹功能 */ voID FingerClosed(); /** * 不支持指纹功能 */ voID doNotSupportFinger(); }}
这个是工具类,下面上使用方法
package com.tsm.test;import androID.app.Activity;import androID.content.Intent;import androID.os.Build;import androID.os.Bundle;import androID.util.Log;import androID.vIEw.VIEw;import androID.Widget.button;import androID.Widget.Toast;public class MainActivity extends Activity implements FingerPrintUiHelper.FingerPrintCallBack { private FingerPrintUiHelper fingerPrintUiHelper; @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_main); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {////指纹功能是23之后的版本才有的 initFingerPrint(); button button = (button) findVIEwByID(R.ID.button); assert button != null; button.setonClickListener(new VIEw.OnClickListener() { @OverrIDe public voID onClick(VIEw v) { fingerPrintUiHelper.startFingerPrintListen(); } }); findVIEwByID(R.ID.btn).setonClickListener(new VIEw.OnClickListener() { @OverrIDe public voID onClick(VIEw v) { fingerPrintUiHelper.stopsFingerPrintListen(); } }); } } private voID initFingerPrint() { fingerPrintUiHelper = new FingerPrintUiHelper(this,this); } @OverrIDe protected voID onActivityResult(int requestCode,int resultCode,Intent data) { if (requestCode == FingerPrintUiHelper.REQUEST_CODE_CONFIRM_DEVICE_CREDENTIALS) { // Challenge completed,proceed with using cipher if (resultCode == RESulT_OK) { Toast.makeText(this,"识别成功",Toast.LENGTH_SHORT).show();// jumpToMain2Activity(); } else { Toast.makeText(this,"识别失败",Toast.LENGTH_SHORT).show(); } } } @OverrIDe protected voID onDestroy() { if (fingerPrintUiHelper != null) fingerPrintUiHelper.stopsFingerPrintListen(); super.onDestroy(); } /** * 成功 */ @OverrIDe public voID onAuthenticationSucceeded() { Toast.makeText(this,Toast.LENGTH_SHORT).show(); } @OverrIDe public voID onAuthenticationFailed(int count) { String msg = "您还可以尝试%d次"; Toast.makeText(this,String.format(msg,count),Toast.LENGTH_SHORT).show(); } /** * 验证失败,走密码验证 */ @OverrIDe public voID onAuthenticationError() { FingerPrintUiHelper.jumpToGesturePassCheck(this); } /** * 没有指纹功能 */ @OverrIDe public voID FingerClosed() { //Todo 可以写一个Dialog跳转设置页,这里我就不写了 Toast.makeText(this,"指纹功能已关闭",Toast.LENGTH_SHORT).show(); } @OverrIDe public voID doNotSupportFinger() { Log.i("info","-------------doNotSupportFinger--------------------"); Toast.makeText(this,"该设备不支持指纹功能",Toast.LENGTH_SHORT).show(); } }
最后添加权限:
<uses-permission androID:name="androID.permission.USE_FINGERPRINT" />
总结
以上所示是小编给大家介绍的AndroID 指纹功能实例代码,希望对大家有所帮助,如果大家有任何疑问欢迎给我留言,小编会及时回复大家的!
总结以上是内存溢出为你收集整理的Android 指纹功能实例代码全部内容,希望文章能够帮你解决Android 指纹功能实例代码所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)