AndroID从6.0系统开始就支持指纹认证功能了,指纹功能还需要有硬件支持才行
指纹与手机系统设置的指纹进行匹配
如图:
在LoginActivity 中d出指纹验证Fragment,验证成功进入MainActivity中
代码:
创建FingerprintDialogFragment 继承 DialogFragment
package com.chuanye.zhiwendemo;import androID.annotation.Suppresslint;import androID.content.Context;import androID.harDWare.fingerprint.FingerprintManager;import androID.os.Bundle;import androID.os.CancellationSignal;import androID.support.annotation.Nullable;import androID.support.v4.app.DialogFragment;import androID.vIEw.LayoutInflater;import androID.vIEw.VIEw;import androID.vIEw.VIEwGroup;import androID.Widget.TextVIEw;import androID.Widget.Toast;import javax.crypto.Cipher;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(); } @Suppresslint("NewAPI") @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(); } @Suppresslint({"MissingPermission", "NewAPI"}) 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); } @Suppresslint("NewAPI") private voID stopListening() { if (mCancellationSignal != null) { mCancellationSignal.cancel(); mCancellationSignal = null; isSelfCancelled = true; } }}
布局
<?xml version="1.0" enCoding="utf-8"?><linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" androID:orIEntation="vertical" androID:layout_wIDth="match_parent" androID:layout_height="match_parent"> <ImageVIEw androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:layout_gravity="center_horizontal" androID:src="@drawable/ic_fp_40px" /> <TextVIEw androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:layout_gravity="center" androID:layout_margintop="20dp" androID:text="请验证指纹解锁" androID:textcolor="#000" androID:textSize="16sp" /> <TextVIEw androID:ID="@+ID/error_msg" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:layout_gravity="center" androID:layout_margintop="5dp" androID:maxlines="1" androID:textSize="12sp" androID:textcolor="#f45" /> <VIEw androID:layout_wIDth="match_parent" androID:layout_height="0.5dp" androID:layout_margintop="10dp" androID:background="#ccc" /> <TextVIEw androID:ID="@+ID/cancel" androID:layout_wIDth="match_parent" androID:layout_height="50dp" androID:gravity="center" androID:text="取消" androID:textcolor="#5d7883" androID:textSize="16sp" /></linearLayout>
LoginActivity 中
package com.chuanye.zhiwendemo;import androID.annotation.Suppresslint;import androID.annotation.TargetAPI;import androID.app.KeyguardManager;import androID.content.Intent;import androID.harDWare.fingerprint.FingerprintManager;import androID.os.Build;import androID.security.keystore.KeyGenParameterSpec;import androID.security.keystore.KeyPropertIEs;import androID.support.v7.app.AppCompatActivity;import androID.os.Bundle;import androID.Widget.Toast;import java.security.KeyStore;import javax.crypto.Cipher;import javax.crypto.KeyGenerator;import javax.crypto.SecretKey;public class LoginActivity extends AppCompatActivity { //https://blog.csdn.net/guolin_blog/article/details/81450114 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(); } } /** * 判断是否支持指纹 * @return */ @Suppresslint("MissingPermission") 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(getSupportFragmentManager(), "fingerprint"); } public voID onAuthenticated() { Intent intent = new Intent(this, MainActivity.class); startActivity(intent); finish(); }}
activity_main.xml布局
<?xml version="1.0" enCoding="utf-8"?><FrameLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" androID:layout_wIDth="match_parent" androID:layout_height="match_parent"> <TextVIEw androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:text="已进入App主界面" androID:textSize="18sp" androID:layout_gravity="center" /></FrameLayout>
最后添加权限
<uses-permission androID:name="androID.permission.USE_FINGERPRINT"></uses-permission>
注意,通常为了让用户清楚的知道现在需要进行指纹认证,Google官方建议最好使用一个通用的指纹图标,而不应该由各APP制作自己的指纹图标。为此,Google也特意提供了一套指纹认证的组图,可以 点击这里 查看和下载。
指纹认证不能单独使用,必须要配合着图案或其他认证方式一起来使用,因为一定要提供一个在设备不支持指纹情况下的其他认证方式
另外FingerprintManager在最新的AndroID 9.0系统上已经被废弃了因为AndroID 9.0系统提供了更加强大的生物识别认证功能,包括指纹识别、面部识别、甚至是虹膜识别等等,因此仅仅只能用于指纹识别的FingerprintManager已经不能满足新系统的强大需求了。
参考与郭神的https://blog.csdn.net/guolin_blog/article/details/81450114
总结
以上是内存溢出为你收集整理的Android 中指纹识别全部内容,希望文章能够帮你解决Android 中指纹识别所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)