欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

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

程序员文章站 2024-03-06 09:01:25
activity_main.xml源码

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 指纹识别的朋友,谢谢大家对本站的支持!