Android设备指纹认证

Android设备指纹认证介绍一个最简单的方式进行认证指纹 老规矩文章最后附上 DEMO 现在很多设备都还没有真正的将指纹使用起来 用得最多就是屏幕解锁 和微信支付宝的支付时使用 其他应用发现很少使用 其实指纹认证目前已经很成熟 这里用代码来介绍 登录界面 指纹认证 gt 指纹认证成功主界面 1 登录界面 https blog csdn net generallizho pub

介绍一个最简单的方式进行认证指纹,老规矩文章最后附上DEMO

现在很多设备都还没有真正的将指纹使用起来,用得最多就是屏幕解锁,和微信支付宝的支付时使用,其他应用发现很少使用,其实指纹认证目前已经很成熟。

这里用代码来介绍:登录界面(指纹认证)->指纹认证成功主界面

1、登录界面

/ * https://blog.csdn.net/generallizhong */ public class LoginActivity extends Activity { private static final String DEFAULT_KEY_NAME = "default_key"; KeyStore keyStore; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_login); if (supportFingerprint()) { initKey(); initCipher(); } } //检测设备 public boolean supportFingerprint() { if (Build.VERSION.SDK_INT < 23) { Toast.makeText(this, "您的系统版本过低,不支持指纹功能", Toast.LENGTH_SHORT).show(); return false; } else { KeyguardManager keyguardManager = getSystemService(KeyguardManager.class); FingerprintManager fingerprintManager = getSystemService(FingerprintManager.class); if (!fingerprintManager.isHardwareDetected()) { Toast.makeText(this, "您的手机不支持指纹功能", Toast.LENGTH_SHORT).show(); return false; } else if (!keyguardManager.isKeyguardSecure()) { Toast.makeText(this, "您还未设置锁屏,请先设置锁屏并添加一个指纹", Toast.LENGTH_SHORT).show(); return false; } else if (!fingerprintManager.hasEnrolledFingerprints()) { Toast.makeText(this, "您至少需要在系统设置中添加一个指纹", Toast.LENGTH_SHORT).show(); return false; } } return true; } @TargetApi(23) private void initKey() { try { keyStore = KeyStore.getInstance("AndroidKeyStore"); keyStore.load(null); KeyGenerator keyGenerator = KeyGenerator.getInstance(KeyProperties.KEY_ALGORITHM_AES, "AndroidKeyStore"); KeyGenParameterSpec.Builder builder = new KeyGenParameterSpec.Builder(DEFAULT_KEY_NAME, KeyProperties.PURPOSE_ENCRYPT | KeyProperties.PURPOSE_DECRYPT) .setBlockModes(KeyProperties.BLOCK_MODE_CBC) .setUserAuthenticationRequired(true) .setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_PKCS7); keyGenerator.init(builder.build()); keyGenerator.generateKey(); } catch (Exception e) { throw new RuntimeException(e); } } @TargetApi(23) private void initCipher() { try { SecretKey key = (SecretKey) keyStore.getKey(DEFAULT_KEY_NAME, null); Cipher cipher = Cipher.getInstance(KeyProperties.KEY_ALGORITHM_AES + "/" + KeyProperties.BLOCK_MODE_CBC + "/" + KeyProperties.ENCRYPTION_PADDING_PKCS7); cipher.init(Cipher.ENCRYPT_MODE, key); showFingerPrintDialog(cipher); } catch (Exception e) { throw new RuntimeException(e); } } private void showFingerPrintDialog(Cipher cipher) { FingerprintDialogFragment fragment = new FingerprintDialogFragment(); fragment.setCipher(cipher); fragment.show(getFragmentManager(), "fingerprint"); } public void onAuthenticated() { Intent intent = new Intent(this, MainActivity.class); startActivity(intent); finish(); }

首先在onCreate()方法中,调用了supportFingerprint()方法来判断当前设备是否支持指纹认证功能。这一点是非常重要的,因为当设备不支持指纹认证的时候,还需要及时切换到如图案、密码等其他的认证方式。

当设备支持指纹认证的时候,再分为两步,第一步生成一个对称加密的Key,第二步生成一个Cipher对象,这都是Android指纹认证API要求的标准用法。得到了Cipher对象之后,我们创建FingerprintDialogFragment的实例,并将Cipher对象传入,再将FingerprintDialogFragment显示出来就可以了。

2、指纹认证的相关逻辑在这里完成

public class FingerprintDialogFragment extends DialogFragment { private FingerprintManager fingerprintManager; private CancellationSignal mCancellationSignal; private Cipher mCipher; private LoginActivity mActivity; private TextView errorMsg; / * 标识是否是用户主动取消的认证。 */ private boolean isSelfCancelled; public void setCipher(Cipher cipher) { mCipher = cipher; } @Override public void onAttach(Context context) { super.onAttach(context); mActivity = (LoginActivity) getActivity(); } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); fingerprintManager = getContext().getSystemService(FingerprintManager.class); setStyle(DialogFragment.STYLE_NORMAL, android.R.style.Theme_Material_Light_Dialog); } @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) { View v = inflater.inflate(R.layout.fingerprint_dialog, container, false); errorMsg = v.findViewById(R.id.error_msg); TextView cancel = v.findViewById(R.id.cancel); cancel.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { dismiss(); stopListening(); } }); return v; } @Override public void onResume() { super.onResume(); // 开始指纹认证监听 startListening(mCipher); } @Override public void onPause() { super.onPause(); // 停止指纹认证监听 stopListening(); } //监听认证成功与失败 private void startListening(Cipher cipher) { isSelfCancelled = false; mCancellationSignal = new CancellationSignal(); fingerprintManager.authenticate(new FingerprintManager.CryptoObject(cipher), mCancellationSignal, 0, new FingerprintManager.AuthenticationCallback() { @Override public void onAuthenticationError(int errorCode, CharSequence errString) { if (!isSelfCancelled) { errorMsg.setText(errString); if (errorCode == FingerprintManager.FINGERPRINT_ERROR_LOCKOUT) { Toast.makeText(mActivity, errString, Toast.LENGTH_SHORT).show(); dismiss(); } } } @Override public void onAuthenticationHelp(int helpCode, CharSequence helpString) { errorMsg.setText(helpString); } @Override public void onAuthenticationSucceeded(FingerprintManager.AuthenticationResult result) { Toast.makeText(mActivity, "指纹认证成功", Toast.LENGTH_SHORT).show(); mActivity.onAuthenticated();//认证成功后就可以写成功后的代码逻辑 } @Override public void onAuthenticationFailed() { errorMsg.setText("指纹认证失败,请再试一次"); } }, null); } private void stopListening() { if (mCancellationSignal != null) { mCancellationSignal.cancel(); mCancellationSignal = null; isSelfCancelled = true; } } 

 

最后的最后,当指纹认证成功之后,会在FingerprintDialogFragment的回调当中调用LoginActivity的onAuthenticated()方法,然后界面会跳转到MainActivity

在使用中可能会出现很多开发或厂商自行使用指纹认证图片,这样就造成五花八门的指纹认证如,建议统一使用官方提供图标图标下载,

Android设备指纹认证

文中的指纹认证Demo实现过程很简单,但是切记它是不能单独使用的,必须要配合着图案或其他认证方式一起来使用,因为一定要提供一个在设备不支持指纹情况下的其他认证方式

DEMO下载:

CSDN:下载

百度网盘:下载   提取码: 447q

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/206907.html原文链接:https://javaforall.net

(0)
上一篇 2026年3月19日 下午3:13
下一篇 2026年3月19日 下午3:14


相关推荐

  • linux压缩文件夹,cksum比较两个文件或文件夹等是否一样

    linux压缩文件夹,cksum比较两个文件或文件夹等是否一样linux压缩文件夹,cksum比较两个文件或文件夹等是否一样

    2022年4月23日
    85
  • pytorch+Pycharm安装配置

    pytorch+Pycharm安装配置1 普通环境安装 安装后不用 pytorch 这种安装只可以使用 numpy 却不能使用 torch 当然这是在 pycharm 的实验结果 当作一般学习也是可以的 numpy 和 torch 的区别最主要是能否使用显卡算力 所以一般学习可以使用 numpy 不用去配置 pytorch 贴上 pytorch 官网连接 PyTorch 明显看到 我的 pytorch 是已经 alreadyinsta 的 但是我还是又出现了下面情况 所以 普通环境 win10 直接安装 pytorch 是没有必要的 就是浪费磁盘 2 设置

    2026年3月27日
    3
  • 1997元!阿里千问AI眼镜来了,再造一个超级入口?

    1997元!阿里千问AI眼镜来了,再造一个超级入口?

    2026年3月12日
    2
  • FASTAI-fastai 学习笔记——lesson1[通俗易懂]

    FASTAI-fastai 学习笔记——lesson1[通俗易懂]fastai学习笔记——lesson10-重要的参考网站课程一详细笔记(https://github.com/hiromis/notes/blob/master/Lesson1.md)课程一视频(https://www.bilibili.com/video/av41718196/?p=1)课程一源码(https://github.com/fastai/course-v3/blob…

    2025年10月3日
    4
  • 什么是线程? [通俗易懂]

    什么是线程? [通俗易懂]所有重要的操作系统都支持进程的概念–独立运行的程序,在某种程度上相互隔离。线程有时称为轻量级进程。与进程一样,它们拥有通过程序运行的独立的并发路径,并且每个线程都有自己的程序计数器,称为堆栈

    2022年7月1日
    26
  • 二叉树的前中后和层序遍历详细图解(递归和非递归写法)「建议收藏」

    二叉树的前中后和层序遍历详细图解(递归和非递归写法)「建议收藏」我家门前有两棵树,一棵是二叉树,另一棵也是二叉树。遍历一棵二叉树常用的有四种方法,前序(PreOrder)、中序(InOrder)、后序(PastOrder)还有层序(LevelOrder)。前中后序三种遍历方式都是以根节点相对于它的左右孩子的访问顺序定义的。例如根-&gt;左-&gt;右便是前序遍历,左-&gt;根-&gt;右便是中序遍历,左-&gt;右-&gt;根…

    2022年5月22日
    39

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号