android  指纹识别调用实现方法及示例代码

activity_main.xml源码

<?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:layout_width="match_parent" 
  android:layout_height="match_parent" 
  android:gravity="center" 
  android:orientation="vertical" 
  android:paddingBottom="@dimen/activity_vertical_margin" 
  android:paddingLeft="@dimen/activity_horizontal_margin" 
  android:paddingRight="@dimen/activity_horizontal_margin" 
  android:paddingTop="@dimen/activity_vertical_margin" 
  tools:context="com.liu.finger.MainActivity"> 
 
  <TextView 
    android:id="@+id/textView" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Hello World!" 
    android:textSize="18sp" /> 
 
  <Button 
    android:id="@+id/btn_activity_main_finger" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentStart="true" 
    android:layout_below="@+id/textView" 
    android:layout_marginTop="54dp" 
    android:text="指纹识别" /> 
</LinearLayout> 

MainActivity.java源码

package com.liu.finger; 
 
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.util.Log; 
import android.view.View; 
import android.widget.Button; 
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"; 
 
  @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); 
    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(); 
    } 
 
    @Override 
    public void onAuthenticationFailed() { 
      Toast.makeText(MainActivity.this, "指纹识别失败", Toast.LENGTH_SHORT).show(); 
    } 
  }; 
 
 
  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(); 
      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(); 
      } else { 
        Toast.makeText(this, "识别失败", Toast.LENGTH_SHORT).show(); 
      } 
    } 
  } 
 
  private void Log(String tag, String msg) { 
    Log.d(tag, msg); 
  } 
} 

通过此文希望能帮助开发Android 指纹识别的朋友,谢谢大家对本站的支持!

声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:notice#cainiaojc.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。