十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
本篇文章为大家展示了Android中怎么实现指纹功能,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。
创新互联公司服务项目包括新蔡网站建设、新蔡网站制作、新蔡网页制作以及新蔡网络营销策划等。多年来,我们专注于互联网行业,利用自身积累的技术优势、行业经验、深度合作伙伴关系等,向广大中小型企业、政府机构等提供互联网行业的解决方案,新蔡网站推广取得了明显的社会效益与经济效益。目前,我们服务的客户以成都为中心已经辐射到新蔡省份的部分城市,未来相信会继续扩大服务区域并继续获得客户的支持与信任!
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. * * 指纹识别功能 * * 如果创建了该类的实例,必须调用 stopsFingerPrintListen 方法 * * 添加权限 ** */ 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(); } }
最后添加权限:
上述内容就是Android中怎么实现指纹功能,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注创新互联行业资讯频道。