在onCreate()中重复验证FingerPrint,可以通过以下步骤实现:
- 导入所需的类和库:import android.Manifest;
import android.content.pm.PackageManager;
import android.hardware.fingerprint.FingerprintManager;
import android.os.Build;
import android.os.CancellationSignal;
import android.support.annotation.RequiresApi;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
- 检查设备是否支持指纹识别:private boolean isFingerprintSensorAvailable() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
FingerprintManager fingerprintManager = (FingerprintManager) getSystemService(FINGERPRINT_SERVICE);
if (fingerprintManager != null && fingerprintManager.isHardwareDetected()) {
return true;
}
}
return false;
}
- 请求指纹识别权限:private void requestFingerprintPermission() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.USE_FINGERPRINT) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.USE_FINGERPRINT}, REQUEST_FINGERPRINT_PERMISSION);
}
}
}
- 实现指纹识别回调:private void startFingerprintAuthentication() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
FingerprintManager fingerprintManager = (FingerprintManager) getSystemService(FINGERPRINT_SERVICE);
CancellationSignal cancellationSignal = new CancellationSignal();
fingerprintManager.authenticate(null, cancellationSignal, 0, new FingerprintManager.AuthenticationCallback() {
@Override
public void onAuthenticationError(int errorCode, CharSequence errString) {
super.onAuthenticationError(errorCode, errString);
// 处理认证错误
}
public void onAuthenticationHelp(int helpCode, CharSequence helpString) {
super.onAuthenticationHelp(helpCode, helpString);
public void onAuthenticationSucceeded(FingerprintManager.AuthenticationResult result) {
super.onAuthenticationSucceeded(result);
public void onAuthenticationFailed() {
super.onAuthenticationFailed();
}
- 在onCreate()方法中调用上述方法:@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (isFingerprintSensorAvailable()) {
requestFingerprintPermission();
startFingerprintAuthentication();
}
这样,在onCreate()中重复验证FingerPrint的过程就完成了。请注意,上述代码仅适用于Android 6.0(API级别23)及以上的设备。在实际使用中,还需要根据具体需求进行错误处理、用户提示等操作。