先看以下效果图(图中有几次的失败,不是因为它的识别率不高,是因为我用没录过的指纹测试):
实现的方法很简单,不用引入依赖,它自带实现指纹的类,这里我就直接贴出代码(MainActivity.java):
1 public class MainActivity extends AppCompatActivity implements View.OnClickListener{ 2 3 private ImageView btnImg; 4 private TextView txt; 5 private FingerprintManager fingerprintManager = null; 6 7 @Override 8 protected void onCreate(Bundle savedInstanceState) { 9 super.onCreate(savedInstanceState); 10 setContentView(R.layout.activity_main); 11 initView(); 12 } 13 14 private void initView(){ 15 btnImg = (ImageView)findViewById(R.id.btnImg); 16 btnImg.setOnClickListener(this); 17 txt = (TextView)findViewById(R.id.txt); 18 } 19 20 @Override 21 public void onClick(View view) { 22 //隐藏点击的图片控件 23 btnImg.setVisibility(View.GONE); 24 //隐藏显示的文字 25 txt.setVisibility(View.GONE); 26 //获取系统服务对象 27 fingerprintManager = (FingerprintManager)getSystemService(Context.FINGERPRINT_SERVICE); 28 //检测是否有指纹识别的硬件 29 if(!fingerprintManager.isHardwareDetected()){ 30 Toast.makeText(this, "您的手机没有指纹功能", Toast.LENGTH_SHORT).show(); 31 return; 32 } 33 34 //检查设备是否处于安全状态中 35 KeyguardManager keyguardManager = (KeyguardManager)getSystemService(Context.KEYGUARD_SERVICE); 36 if(!keyguardManager.isKeyguardSecure()){ 37 //如果不是出于安全状态中,跳转打开安全保护(锁屏等) 38 return; 39 } 40 41 //检测系统中是否注册的指纹 42 if(!fingerprintManager.hasEnrolledFingerprints()){ 43 //没有录入指纹,跳转到指纹录入 44 Toast.makeText(this, "没有录入指纹", Toast.LENGTH_SHORT).show(); 45 return; 46 } 47 48 //开始指纹识别 49 fingerprintManager.authenticate(null,cancellationSignal,0,myAuthCallback,null); 50 } 51 52 //初始化cancellationSignal类 53 private CancellationSignal cancellationSignal = new CancellationSignal(); 54 55 //初始化MyAuthCallback 56 private FingerprintManager.AuthenticationCallback myAuthCallback = new FingerprintManager.AuthenticationCallback() { 57 @Override 58 public void onAuthenticationSucceeded(FingerprintManager.AuthenticationResult result) { 59 super.onAuthenticationSucceeded(result); 60 Toast.makeText(MainActivity.this, "识别成功", Toast.LENGTH_SHORT).show(); 61 txt.setVisibility(View.VISIBLE); 62 txt.setText("识别成功"); 63 txt.setTextColor(Color.parseColor("#01CCFF")); 64 btnImg.setVisibility(View.VISIBLE); 65 } 66 67 @Override 68 public void onAuthenticationFailed() { 69 super.onAuthenticationFailed(); 70 Toast.makeText(MainActivity.this, "识别失败,请重试", Toast.LENGTH_SHORT).show(); 71 txt.setText("识别失败"); 72 txt.setTextColor(Color.parseColor("#D81B60")); 73 txt.setVisibility(View.VISIBLE); 74 } 75 }; 76 }
然后就是布局文件的代码:
1 <?xml version="1.0" encoding="utf-8"?> 2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:app="http://schemas.android.com/apk/res-auto" 4 xmlns:tools="http://schemas.android.com/tools" 5 android:layout_width="match_parent" 6 android:layout_height="match_parent" 7 tools:context=".MainActivity"> 8 9 <TextView 10 android:textSize="16sp" 11 android:textStyle="bold" 12 android:gravity="center" 13 android:text="指纹认证" 14 android:textColor="#FFF" 15 android:background="#01CCFF" 16 android:layout_width="match_parent" 17 android:layout_height="45dp" /> 18 19 <TextView 20 android:visibility="gone" 21 android:id="@+id/txt" 22 android:text="识别成功" 23 android:gravity="center" 24 android:textSize="20sp" 25 android:textColor="#01CCFF" 26 android:layout_width="match_parent" 27 android:layout_height="match_parent" /> 28 29 <ImageView 30 android:id="@+id/btnImg" 31 android:src="@drawable/zhiwen" 32 android:layout_marginBottom="100dp" 33 android:layout_alignParentBottom="true" 34 android:layout_centerInParent="true" 35 android:scaleType="fitXY" 36 android:layout_width="60dp" 37 android:layout_height="60dp" /> 38 </RelativeLayout>