如何实现指纹验证?
Android6.0之后就已经开始支持指纹验证了,也就是说Android自带功能。
指纹验证需要哪些注意事项?
(1)Android系统版本大于等于Android6.0;
(2)测试设备是否具有指纹功能。
效果图如下:
步骤如下:
(1)指纹验证对话框布局文件 layout_fingerprint.xml 代码如下:
(2)创建自定义的FragmentDialog对话框来实现相关的监听操作。
public class FingerprintDialogFragment extends DialogFragment {
private TextView tvErrorInfo, tvCancel;
private LoginActivity loginActivity;
private FingerprintManager fingerprintManager;
private CancellationSignal cancellationSignal;
private Cipher cipher;
private boolean isUserCancel;
public void setCipher(Cipher cipher) {
this.cipher = cipher;
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
loginActivity = (LoginActivity) getActivity();
}
@TargetApi(Build.VERSION_CODES.M)
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
fingerprintManager = getContext().getSystemService(FingerprintManager.class);
setStyle(DialogFragment.STYLE_NORMAL, R.style.Translucent_NoTitle);
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.layout_fingerprint, container);
tvErrorInfo = view.findViewById(R.id.FingerPrint_tvErrorInfo);
tvCancel = view.findViewById(R.id.FingerPrint_tvCancel);
tvCancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dismiss();
stopListener();
}
});
return view;
}
@Override
public void onResume() {
super.onResume();
startListener(cipher);
}
@Override
public void onPause() {
super.onPause();
stopListener();
}
/**
* 开始指纹认证监听
*
* @param mCipher
*/
@TargetApi(Build.VERSION_CODES.M)
private void startListener(Cipher mCipher) {
isUserCancel = false;
cancellationSignal = new CancellationSignal();
fingerprintManager.authenticate(new FingerprintManager.CryptoObject(mCipher), cancellationSignal, 0, new FingerprintManager.AuthenticationCallback() {
@Override
public void onAuthenticationError(int errorCode, CharSequence errString) {
if (!isUserCancel) {
tvErrorInfo.setText(errString);
if (errorCode == FingerprintManager.FINGERPRINT_ERROR_LOCKOUT) {
Toast.makeText(loginActivity, errString, Toast.LENGTH_SHORT).show();
dismiss();
}
}
}
@Override
public void onAuthenticationHelp(int helpCode, CharSequence helpString) {
tvErrorInfo.setText(helpString);
}
@Override
public void onAuthenticationSucceeded(FingerprintManager.AuthenticationResult result) {
dismiss();
Toast.makeText(loginActivity, "指纹验证成功", Toast.LENGTH_SHORT).show();
loginActivity.goMainActivity();
}
@Override
public void onAuthenticationFailed() {
tvErrorInfo.setText("指纹验证失败,请再试一次");
}
}, null);
}
/**
* 停止指纹认证监听
*/
private void stopListener() {
if (cancellationSignal != null) {
cancellationSignal.cancel();
cancellationSignal = null;
isUserCancel = true;
}
}
}
(3)在相应的Activity界面调用FragmentDialog对话框,相关代码如下:
public class LoginActivity extends AppCompatActivity implements View.OnClickListener {
private final static String DEFAULT_KEY_NAME = "MyFingerPrint";
private Context mContext;
private Button btnConfirm;
private KeyStore keyStore;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_anotherlogin);
baseDataInit();
bindViews();
viewsAddListener();
}
@Override
protected void onDestroy() {
super.onDestroy();
}
private void baseDataInit() {
mContext = this;
}
private void bindViews() {
btnConfirm = findViewById(R.id.AnotherLogin_btnConfirm);
}
private void viewsAddListener() {
btnConfirm.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.AnotherLogin_btnConfirm: {
if (isSupportFingerPrint()) {
keyInit();
cipherInit();
}
break;
}
default:break;
}
}
/**
* 检查设备是否支持指纹解锁功能
*
* @return
*/
private boolean isSupportFingerPrint() {
if (Build.VERSION.SDK_INT < 23) {
Toast.makeText(mContext, "您的系统版本过低,不支持指纹功能", Toast.LENGTH_SHORT).show();
return false;
} else {
KeyguardManager keyguardManager = getSystemService(KeyguardManager.class);
FingerprintManager fingerprintManager = getSystemService(FingerprintManager.class);
if (!fingerprintManager.isHardwareDetected()) {
Toast.makeText(mContext, "您的手机不支持指纹功能", Toast.LENGTH_SHORT).show();
return false;
} else if (!keyguardManager.isKeyguardSecure()) {
Toast.makeText(mContext, "您还未设置锁屏,请先设置锁屏并添加一个指纹", Toast.LENGTH_SHORT).show();
return false;
} else if (!fingerprintManager.hasEnrolledFingerprints()) {
Toast.makeText(mContext, "您至少需要在系统设置中添加一个指纹", Toast.LENGTH_SHORT).show();
return false;
}
}
return true;
}
/**
* 初始化Keystore
*/
@TargetApi(Build.VERSION_CODES.M)
private void keyInit() {
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);
}
}
/**
* 初始化密码
*/
private void cipherInit() {
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);
}
}
/**
* 显示指纹验证对话框
*
* @param cipher
*/
private void showFingerPrintDialog(Cipher cipher) {
FingerprintDialogFragment fragment = new FingerprintDialogFragment();
fragment.setCipher(cipher);
fragment.show(getFragmentManager(), "fingerprint");
}
/**
* 进入主界面
*/
public void goMainActivity() {
startActivity(new Intent(mContext, MainActivity.class));
}
}
相应的Demo地址为:https://download.csdn.net/download/lpcrazyboy/11225686