Android指纹解锁 Android指纹解锁方法解析
qq_31587497 人气:0想了解Android指纹解锁方法解析的相关内容吗,qq_31587497在本文为您仔细讲解Android指纹解锁的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:Android指纹解锁,Android指纹识别,Android指纹解锁识别,下面大家一起来学习吧。
我先说说这两种的方式的不同之处吧
第一种:
在调动成功之后 不会让你指纹解锁 而是调转到当初你设置指纹解锁时的 手势解锁页面
第二种:
在调动成功之后,是进行指纹解锁 不调转 你直接把手指放到金属感应环 上面进行指纹验证
大家可以根据需求 自行选择
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, "没有开启锁屏密码", Toast.LENGTH_SHORT).show(); return false; } Log(TAG, "已开启锁屏密码"); //判断是否有指纹录入 if (!manager.hasEnrolledFingerprints()) { Toast.makeText(this, "没有录入指纹", Toast.LENGTH_SHORT).show(); return false; } Log(TAG, "已录入指纹"); 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, Manifest.permission.USE_FINGERPRINT) != PackageManager.PERMISSION_GRANTED) { Toast.makeText(this, "没有指纹识别权限", Toast.LENGTH_SHORT).show(); return; } manager.authenticate(cryptoObject, mCancellationSignal, 0, 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, 0, 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; @Override protected 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(); } } @Override public void success() { setToast("识别成功"); } @Override public void error(String error) { setToast(error); } @Override public void help(String msg) { setToast(msg); } @Override public void cancel() { setToast("取消指纹识别"); } @Override public void failure() { setToast("指纹识别失败"); } public void setToast(String msg) { toast.setText("提示:" + msg); }
加载全部内容