android 指纹功能 Android 指纹功能实例代码
开车能上树 人气:0想了解Android 指纹功能实例代码的相关内容吗,开车能上树在本文为您仔细讲解android 指纹功能的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:android,指纹功能,下面大家一起来学习吧。
最近在做项目的时候遇到了添加打开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, 0, 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 指纹功能实例代码,希望对大家有所帮助,如果大家有任何疑问欢迎给我留言,小编会及时回复大家的!
加载全部内容