项目需要实现指纹登录。现弄一个能跑的案例:
开发工具:Android studio
需要注意以下几点:
1 判断设备的API是否大于22,
2.是否有指纹识别模块,
3.判断设备是否设置了锁屏密码,
4.是否录入指纹
添加权限:
<uses-permission android:name="android.permission.USE_FINGERPRINT"></uses-permission><uses-permission android:name="android.permission.WRITE_SETTINGS"></uses-permission>
第二步:界面:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.cordovatest.shanghai.figuretest4.MainActivity"> <ImageView android:id="@+id/image" android:src="@drawable/fingerprint_" android:layout_width="80dp" android:layout_marginTop="100dp" android:layout_gravity="center_horizontal" android:layout_height="80dp" /> <TextView android:id="@+id/start_tv" android:layout_width="100dp" android:text="开始" android:textSize="18sp" android:layout_marginTop="30dp" android:gravity="center" android:layout_gravity="center_horizontal" android:background="@color/colorPrimary" android:layout_height="50dp" /> <TextView android:id="@+id/cancel_tv" android:layout_width="100dp" android:text="取消" android:background="@color/colorPrimary" android:textSize="18sp" android:layout_marginTop="30dp" android:gravity="center" android:layout_gravity="center_horizontal" android:layout_height="50dp" /> </LinearLayout>第三步:Activity
package com.cordovatest.shanghai.figuretest4; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.Manifest; import android.annotation.TargetApi; 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.Build; import android.os.CancellationSignal; import android.support.v4.app.ActivityCompat; import android.view.View; import android.widget.ImageView; import android.widget.TextView; import android.widget.Toast; @TargetApi(Build.VERSION_CODES.M) public class MainActivity extends AppCompatActivity implements View.OnClickListener{ private FingerprintManager manager;//访问指纹硬件的类 private KeyguardManager keyManager;//管理锁屏的类 private CancellationSignal signal = new CancellationSignal(); //此类基于Java加密API的一个包装类,用于防止在指纹扫描中被第三方恶意攻击 private FingerprintManager.CryptoObject cryptoObject; private static final int REQUST_CODE=1; private ImageView image; private String minSdkVersion; private int i=0; private TextView start_tv; private TextView cancel_tv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); image= (ImageView) findViewById(R.id.image); start_tv = (TextView) findViewById(R.id.start_tv); cancel_tv = (TextView) findViewById(R.id.cancel_tv); start_tv.setOnClickListener(this); cancel_tv.setOnClickListener(this); minSdkVersion= Build.VERSION.SDK; } private void initLowSDK() { Toast.makeText(getBaseContext(), "目前指纹识别只支持6.0以上的系统哦!", Toast.LENGTH_LONG).show(); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if(requestCode==REQUST_CODE && resultCode==RESULT_OK){ Toast.makeText(getBaseContext(), "识别成功!", Toast.LENGTH_SHORT).show(); image.setImageResource(R.drawable.fingerprint_); }else{ Toast.makeText(getBaseContext(), "识别失败!", Toast.LENGTH_SHORT).show(); } } /** * 获得系统服务对象的引用 */ private void initFinger() { //通过V4包获得对象 manager = (FingerprintManager) getSystemService(Context.FINGERPRINT_SERVICE); keyManager= (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE); } @TargetApi(Build.VERSION_CODES.M) private boolean isFingerprint(){ //此方法为了保证判断是否支持支持指纹不报错 if (ActivityCompat.checkSelfPermission(this, Manifest.permission.USE_FINGERPRINT) != PackageManager.PERMISSION_GRANTED) { Toast.makeText(getBaseContext(), "没有指纹解锁的权限", Toast.LENGTH_SHORT).show(); return false; } //硬件设备是否支持指纹解锁功能 if (!manager.isHardwareDetected()) { Toast.makeText(getBaseContext(), "该手机不支持指纹解锁", Toast.LENGTH_SHORT).show(); return false; } //判断是否有锁屏密码 if(!keyManager.isKeyguardSecure()){ Toast.makeText(getBaseContext(), "请设置锁屏密码", Toast.LENGTH_SHORT).show(); return false; } //判断是否录入指纹 if (!manager.hasEnrolledFingerprints()) { Toast.makeText(getBaseContext(), "没有录入指纹", Toast.LENGTH_SHORT).show(); return false; } return true; } /** * 开始指纹识别 */ private void startListen() { if (ActivityCompat.checkSelfPermission(this, Manifest.permission.USE_FINGERPRINT) != PackageManager.PERMISSION_GRANTED) { return; } FingerprintManager.AuthenticationCallback callBack=new FingerprintManager.AuthenticationCallback() { @Override public void onAuthenticationError(int errorCode, CharSequence errString) { super.onAuthenticationError(errorCode, errString); Toast.makeText(getBaseContext(), "操作过于频繁,请稍后再试",Toast.LENGTH_SHORT).show(); } @Override public void onAuthenticationHelp(int helpCode, CharSequence helpString) { super.onAuthenticationHelp(helpCode, helpString); } //指纹识别成功 @Override public void onAuthenticationSucceeded(FingerprintManager.AuthenticationResult result) { super.onAuthenticationSucceeded(result); Toast.makeText(getBaseContext(), "指纹识别成功",Toast.LENGTH_SHORT).show(); image.setImageResource(R.drawable.fingerprint_); } //指纹识别失败 @Override public void onAuthenticationFailed() { super.onAuthenticationFailed(); Toast.makeText(getBaseContext(), "指纹识别失败", Toast.LENGTH_SHORT).show(); i++; if(i==3){ Toast.makeText(getBaseContext(), "失败次数过多,请输入锁屏密码", Toast.LENGTH_SHORT).show(); showLockScreenPass(); i=0; } } }; manager.authenticate(cryptoObject, signal, 0, callBack, null); } //取消指纹识别 public void stopListening() { if (signal != null) { // selfCancelled = true; signal.cancel(); signal = null; Toast.makeText(getBaseContext(), "您已经取消指纹识别", Toast.LENGTH_SHORT).show(); } } /** *指纹识别错误次数过多,显示手机锁屏密码 */ private void showLockScreenPass() { Intent intent=keyManager.createConfirmDeviceCredentialIntent("finger","开启锁屏密码"); if(intent!=null){ startActivityForResult(intent, REQUST_CODE); } } @Override public void onClick(View view) { int id = view.getId(); switch (id){ case R.id.start_tv: if(Double.parseDouble(minSdkVersion)>=23) { initFinger(); if (isFingerprint()) { startListen(); } }else{ initLowSDK(); } break; case R.id.cancel_tv: stopListening(); break; default: break; } } } 参考:http://blog.csdn.net/huiling815/article/details/52469483