Android指纹识别需Android6.0以上,且硬件设备支持该功能。本例主要实现的是一个简单的指纹识别。
在使用FingerPrintManager这个类实现的时候发现了很多问题,这个类里面的一些函数是不能调用的,也就是说当前的官方支持其实是有限的,我们能读取到本机已经存在的指纹,然后验证这些指纹。使用FingerPrintManager的时候会遇到要在应用中判断权限的问题,低版本的api无法识别。
主要代码:
package com.example.fingerprinttest;
import android.Manifest;
import android.app.KeyguardManager;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.hardware.fingerprint.FingerprintManager;
import android.os.Bundle;
import android.os.CancellationSignal;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends FragmentActivity {
FingerprintManager manager;
KeyguardManager mKeyManager;
private final static int REQUEST_CODE_CONFIRM_DEVICE_CREDENTIALS = 0;
private final static String TAG = "finger_log";
TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
manager = (FingerprintManager) this.getSystemService(Context.FINGERPRINT_SERVICE);
mKeyManager = (KeyguardManager) this.getSystemService(Context.KEYGUARD_SERVICE);
textView = (TextView) findViewById(R.id.textView);
Button btn_finger = (Button) findViewById(R.id.btn_activity_main_finger);
btn_finger.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (isFinger()) {
Toast.makeText(MainActivity.this, "请进行指纹识别", Toast.LENGTH_LONG).show();
Log(TAG, "keyi");
startListening(null);
}
}
});
}
public boolean isFinger() {
//android studio 上,没有这个会报错
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.USE_FINGERPRINT) != PackageManager.PERMISSION_GRANTED) {
Toast.makeText(this, "没有指纹识别权限", Toast.LENGTH_SHORT).show();
return false;
}
Log(TAG, "有指纹权限");
//判断硬件是否支持指纹识别
if (!manager.isHardwareDetected()) {
Toast.makeText(this, "没有指纹识别模块", Toast.LENGTH_SHORT).show();
return false;
}
Log(TAG, "有指纹模块");
//判断 是否开启锁屏密码
if (!mKeyManager.isKeyguardSecure()) {
Toast.makeText(this, "没有开启锁屏密码", Toast.LENGTH_SHORT).show();
return false;
}
Log(TAG, "已开启锁屏密码");
//判断是否有指纹录入
if (!manager.hasEnrolledFingerprints()) {
Toast.makeText(this, "没有录入指纹", Toast.LENGTH_SHORT).show();
return false;
}
Log(TAG, "已录入指纹");
return true;
}
CancellationSignal mCancellationSignal = new CancellationSignal();
//回调方法
FingerprintManager.AuthenticationCallback mSelfCancelled = new FingerprintManager.AuthenticationCallback() {
@Override
public void onAuthenticationError(int errorCode, CharSequence errString) {
//但多次指纹密码验证错误后,进入此方法;并且,不能短时间内调用指纹验证
Toast.makeText(MainActivity.this, errString, Toast.LENGTH_SHORT).show();
showAuthenticationScreen();
}
@Override
public void onAuthenticationHelp(int helpCode, CharSequence helpString) {
Toast.makeText(MainActivity.this, helpString, Toast.LENGTH_SHORT).show();
}
@Override
public void onAuthenticationSucceeded(FingerprintManager.AuthenticationResult result) {
Toast.makeText(MainActivity.this, "指纹识别成功", Toast.LENGTH_SHORT).show();
textView.setText("指纹识别成功");
}
@Override
public void onAuthenticationFailed() {
Toast.makeText(MainActivity.this, "指纹识别失败", Toast.LENGTH_SHORT).show();
textView.setText("指纹识别失败");
}
};
public void startListening(FingerprintManager.CryptoObject cryptoObject) {
//android studio 上,没有这个会报错
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.USE_FINGERPRINT) != PackageManager.PERMISSION_GRANTED) {
Toast.makeText(this, "没有指纹识别权限", Toast.LENGTH_SHORT).show();
textView.setText("没有指纹识别权限");
return;
}
manager.authenticate(cryptoObject, mCancellationSignal, 0, mSelfCancelled, null);
}
/**
* 锁屏密码
*/
private void showAuthenticationScreen() {
Intent intent = mKeyManager.createConfirmDeviceCredentialIntent("finger", "测试指纹识别");
if (intent != null) {
startActivityForResult(intent, REQUEST_CODE_CONFIRM_DEVICE_CREDENTIALS);
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_CODE_CONFIRM_DEVICE_CREDENTIALS) {
// Challenge completed, proceed with using cipher
if (resultCode == RESULT_OK) {
Toast.makeText(this, "识别成功", Toast.LENGTH_SHORT).show();
textView.setText("指纹识别成功");
} else {
Toast.makeText(this, "识别失败", Toast.LENGTH_SHORT).show();
textView.setText("指纹识别失败");
}
}
}
private void Log(String tag, String msg) {
android.util.Log.d(tag, msg);
}
}
例子较为简单,具体demo下载:http://download.csdn.net/detail/jingzhiying/9753338