我先说说这两种的方式的不同之处吧
第一种:
在调动成功之后 不会让你指纹解锁 而是调转到当初你设置指纹解锁时的 手势解锁页面
第二种:
在调动成功之后,是进行指纹解锁 不调转 你直接把手指放到金属感应环 上面进行指纹验证
大家可以根据需求 自行选择
ok 那就亮代码了
第一种:
xml 布局: 一个 文本显示 一个按钮(不解释)
MainActivity.java源码
public class MainActivity extends FragmentActivity { FingerprintManager manager; KeyguardManager mKeyManager; private final static int REQUEST_CODE_CONFIRM_DEVICE_CREDENTIALS = 0; private final static String TAG = “finger_log”;@OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_main); //1:先获取 指纹识别的的管理者 看看获取方式 是不是感觉就和数学公式一样 (xxx管理者)=this.getSystemService(Context.xxx服务) manager = (FingerprintManager) this.getSystemService(Context.FINGERPRINT_SERVICE); mKeyManager = (KeyguardManager) this.getSystemService(Context.KEyguard_SERVICE); // 2 初始化按钮 设置监听 // 监听 监听 听什么 肯定是听听 这个手机有没有 指纹识别这个功能 所以 看看isFinger() button btn_finger = (button) findVIEwByID(R.ID.btn_activity_main_finger); btn_finger.setonClickListener(new VIEw.OnClickListener() { @OverrIDe public voID onClick(VIEw v) { if (isFinger()) { Toast.makeText(MainActivity.this,"请进行指纹识别",Toast.LENGTH_LONG).show(); Log(TAG,"keyi"); startListening(null); } } }); } public boolean isFinger() { //androID studio 上,没有这个会报错 if (ActivityCompat.checkSelfPermission(this,Manifest.permission.USE_FINGERPRINT) != PackageManager.PERMISSION_GRANTED) { Toast.makeText(this,"没有指纹识别权限",Toast.LENGTH_SHORT).show(); return false; } Log(TAG,"有指纹权限"); //判断硬件是否支持指纹识别 if (!manager.isHarDWareDetected()) { Toast.makeText(this,"没有指纹识别模块",Toast.LENGTH_SHORT).show(); return false; } Log(TAG,"有指纹模块"); //判断 是否开启锁屏密码 if (!mKeyManager.isKeyguardSecure()) { Toast.makeText(this,"没有开启锁屏密码","已开启锁屏密码"); //判断是否有指纹录入 if (!manager.hasEnrolledFingerprints()) { Toast.makeText(this,"没有录入指纹","已录入指纹"); return true; } CancellationSignal mCancellationSignal = new CancellationSignal(); //回调方法 FingerprintManager.AuthenticationCallback mSelfCancelled = new FingerprintManager.AuthenticationCallback() { @OverrIDe public voID onAuthenticationError(int errorCode,CharSequence errString) { //但多次指纹密码验证错误后,进入此方法;并且,不能短时间内调用指纹验证 Toast.makeText(MainActivity.this,errString,Toast.LENGTH_SHORT).show(); showAuthenticationScreen(); } @OverrIDe public voID onAuthenticationHelp(int helpCode,CharSequence helpString) { Toast.makeText(MainActivity.this,helpString,Toast.LENGTH_SHORT).show(); } @OverrIDe public voID onAuthenticationSucceeded(FingerprintManager.AuthenticationResult result) { Toast.makeText(MainActivity.this,"指纹识别成功",Toast.LENGTH_SHORT).show(); } @OverrIDe public voID onAuthenticationFailed() { Toast.makeText(MainActivity.this,"指纹识别失败",Toast.LENGTH_SHORT).show(); } }; public voID startListening(FingerprintManager.CryptoObject cryptoObject) { //androID studio 上,没有这个会报错 if (ActivityCompat.checkSelfPermission(this,Toast.LENGTH_SHORT).show(); return; } manager.authenticate(cryptoObject,mCancellationSignal,mSelfCancelled,null); } /** * 锁屏密码 */ private voID showAuthenticationScreen() { Intent intent = mKeyManager.createConfirmDeviceCredentialintent("finger","测试指纹识别"); if (intent != null) { startActivityForResult(intent,REQUEST_CODE_CONFIRM_DEVICE_CREDENTIALS); } } @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) { Toast.makeText(this,"识别成功",Toast.LENGTH_SHORT).show(); } else { Toast.makeText(this,"识别失败",Toast.LENGTH_SHORT).show(); } } } private voID Log(String tag,String msg) { Log.d(tag,msg); } }
===================================
第二种:
1:xml: 布局 一个文本提示 两个按钮(1: 开始识别 2:取消识别)
2: 咱们将业务 *** 作放到一个FingerprintController 类里面
private static FingerprintController sSingleton = null;private Context mContext;private FingerprintManagerCompat manager;private static final String TAG = "FingerprintController";private static final String PREMISSION = "androID.permission.USE_FINGERPRINT";//成功public static final int FINGER_SUCCESS = 0;//硬件不支持public static final int FINGER_ERROR_NO_HARDWARE = 1;//没有申请权限public static final int FINGER_ERROR_NO_PERMISSION = 2;//用户没有赋予权限//Protection level: normal//指纹权限的级别是normal,理论上不需要动态权限认证public static final int FINGER_ERROR_NO_USER_PERMISSION = 3;//用户没有储存指纹public static final int FINGER_ERROR_NO_FINGER = 4;//取消指纹识别private CancellationSignal cancellationSignal;private FingerAuthListener mAuthListener;public static synchronized FingerprintController getInstance(Context context) { if (sSingleton == null) { sSingleton = new FingerprintController(context); } return sSingleton;}public FingerprintController(Context context) { mContext = context; manager = FingerprintManagerCompat.from(this.mContext);}/** * 开始指纹识别 * 失败次数过多后需要一定时间后才可以重新启动 */public voID startFingerAuth() { if (null == cancellationSignal) { cancellationSignal = new CancellationSignal(); } manager.authenticate(null,cancellationSignal,new FingerAuthCallBack(),null);}/** * 取消指纹识别 */public voID cancelFingerAuth() { if (cancellationSignal != null) { cancellationSignal.cancel(); if (mAuthListener != null) mAuthListener.cancel(); }}/** * 指纹识别回调 */public class FingerAuthCallBack extends FingerprintManagerCompat.AuthenticationCallback { // 当出现错误的时候回调此函数,比如多次尝试都失败了的时候,errString是错误信息 @OverrIDe public voID onAuthenticationError(int errMsgid,CharSequence errString) {// Log.d(TAG,“onAuthenticationError: ” + errString); if (null != mAuthListener) mAuthListener.error(errString.toString()); } // 当指纹验证失败的时候会回调此函数,失败之后允许多次尝试,失败次数过多会停止响应一段时间然后再停止sensor的工作 @OverrIDe public voID onAuthenticationFailed() { if (null != mAuthListener) mAuthListener.failure(); } @OverrIDe public voID onAuthenticationHelp(int helpMsgid,CharSequence helpString) { if (null != mAuthListener) Log.e("helpMsgid",helpMsgid+""); Log.e("helpString",helpString.toString()); mAuthListener.help(helpString.toString()); } // 当验证的指纹成功时会回调此函数,然后不再监听指纹sensor @OverrIDe public voID onAuthenticationSucceeded(FingerprintManagerCompat.AuthenticationResult result) { if (null != mAuthListener) mAuthListener.success(); }}/** * 检查指纹解锁是否可用 * * @return 状态 */public int checkFingerEnable() { if (null == manager) { manager = FingerprintManagerCompat.from(mContext); } if (!isAppPermissionEnable()) { return FINGER_ERROR_NO_PERMISSION; } if (!manager.isHarDWareDetected()) { return FINGER_ERROR_NO_HARDWARE; } if (!manager.hasEnrolledFingerprints()) { return FINGER_ERROR_NO_FINGER; } if (!isUserPermissionEnable()) { return FINGER_ERROR_NO_USER_PERMISSION; } return FINGER_SUCCESS;}/** * 是否声明了该权限 */private boolean isAppPermissionEnable() { PackageManager pm = mContext.getPackageManager(); if (pm == null) { Log.w(TAG,"can't get packagemanager"); return true; } try { return PackageManager.PERMISSION_GRANTED == pm.checkPermission(PREMISSION,mContext.getPackagename()); } catch (Exception e) { Log.w(TAG,"can't checkt Permission " + e.getMessage()); return true; }}/** * 是否具有动态权限,理论上不需要验证 */private boolean isUserPermissionEnable() { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { return PackageManager.PERMISSION_GRANTED == mContext.checkSelfPermission(Manifest.permission.USE_FINGERPRINT); } return true;}public voID setAuthListener(FingerAuthListener authListener) { mAuthListener = authListener;}public interface FingerAuthListener { voID success(); voID error(String error); voID help(String msg); voID cancel(); voID failure();}}
3:在 main中 实现 FingerprintController.FingerAuthListener这个 回调接口
private int code = FingerprintController.FINGER_SUCCESS;private TextVIEw toast;@OverrIDeprotected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_main); toast = (TextVIEw) findVIEwByID(R.ID.toast); code = FingerprintController.getInstance(this).checkFingerEnable(); if (code == FingerprintController.FINGER_SUCCESS) { FingerprintController.getInstance(this).setAuthListener(this); setToast("可以启动指纹识别"); } else { switch (code) { case FingerprintController.FINGER_ERROR_NO_HARDWARE: setToast("该设备不支持指纹识别"); break; case FingerprintController.FINGER_ERROR_NO_PERMISSION: setToast("当前应用没有指纹识别权限"); break; case FingerprintController.FINGER_ERROR_NO_FINGER: setToast("当前设备没有录入指纹,请前往录入指纹"); break; } }}public voID start(VIEw vIEw) { if (code == FingerprintController.FINGER_SUCCESS) { FingerprintController.getInstance(this).startFingerAuth(); setToast("开始指纹识别"); }}public voID cancel(VIEw vIEw) { if (code == FingerprintController.FINGER_SUCCESS) { FingerprintController.getInstance(this).cancelFingerAuth(); }}@OverrIDepublic voID success() { setToast("识别成功");}@OverrIDepublic voID error(String error) { setToast(error);}@OverrIDepublic voID help(String msg) { setToast(msg);}@OverrIDepublic voID cancel() { setToast("取消指纹识别");}@OverrIDepublic voID failure() { setToast("指纹识别失败");}public voID setToast(String msg) { toast.setText("提示:" + msg);}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。
总结以上是内存溢出为你收集整理的Android指纹解锁方法解析全部内容,希望文章能够帮你解决Android指纹解锁方法解析所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)