国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android 指紋功能實例代碼

2019-12-12 01:22:42
字體:
來源:轉載
供稿:網友

最近在做項目的時候遇到了添加打開app圖像解鎖的功能,自己嘴欠說現在都用指紋功能,自己給自己挖了一個坑,真是沒誰了

從網上看了一些資料,但是給我demo考慮的不是很多,設備支不支持都沒考慮,如果支持的話是否添加過指紋也不知道,其實方法都很簡單。

廢話不多說,貼上工具類和使用方法

package com.tsm.test;import android.annotation.TargetApi;import android.app.Activity;import android.app.KeyguardManager;import android.content.Context;import android.content.Intent;import android.os.Build;import android.support.v4.hardware.fingerprint.FingerprintManagerCompat;import android.support.v4.os.CancellationSignal;/** * Created by tsm on 2017/3/20. * <p/> * 指紋識別功能 * * 如果創建了該類的實例,必須調用  stopsFingerPrintListen 方法 * * 添加權限 * <uses-permission android:name="android.permission.USE_FINGERPRINT" /> * */public class FingerPrintUiHelper extends FingerprintManagerCompat.AuthenticationCallback {  private final FingerPrintCallBack callback;  private CancellationSignal signal;  private FingerprintManagerCompat fingerprintManager;  /**   * 如果失敗次數過多,調用系統的startActivityForResult   * 這個是code   */  public static final int REQUEST_CODE_CONFIRM_DEVICE_CREDENTIALS = 10;  /**   * 用于提示用戶還可以嘗試幾次,比較友好   */  private int count;  /**   * 控制是否開啟過指紋功能   */  public boolean isStartFinger;  /**   * 初始化指紋功能   * @param activity   * @param callback   */  public FingerPrintUiHelper(Activity activity, FingerPrintCallBack callback) {    this.callback = callback;    signal = new CancellationSignal();    fingerprintManager = FingerprintManagerCompat.from(activity);    isStartFinger = false;    if (!fingerprintManager.isHardwareDetected()) {      if (callback != null)        callback.doNotSupportFinger();      return;    }    if (!fingerprintManager.hasEnrolledFingerprints()) {      if (callback != null)        callback.FingerClosed();    }  }  /**   * 開始掃描指紋   */  public void startFingerPrintListen() {    count = 5;    isStartFinger = true;    fingerprintManager.authenticate(null, 0, signal, this, null);  }  /**   * 初始化未必調用 startFingerPrintListen   * 所以添加變量控制   */  public void stopsFingerPrintListen() {    if (isStartFinger) {      if (signal != null && !signal.isCanceled()) {        signal.cancel();      }    }  }  /**   * 識別成功   * @param result   */  @Override  public void onAuthenticationSucceeded(FingerprintManagerCompat.AuthenticationResult result) {    if (callback != null)      callback.onAuthenticationSucceeded();  }  /**   * 識別失敗   */  @Override  public void onAuthenticationFailed() {    count--;    if (count > 0) {      if (callback != null)        callback.onAuthenticationFailed(count);      return;    }  }  /**   * 有錯誤   * @param errMsgId   * @param errString   */  @Override  public void onAuthenticationError(int errMsgId, CharSequence errString) {    if (errMsgId == 5) {      if (callback != null)        callback.FingerClosed();      return;    }    if (errMsgId == 7) {      if (callback != null)        callback.onAuthenticationError();      return;    }  }  /**   * 多次調用指紋識別失敗后,調用此方法   *   * @param activity   */  @TargetApi(Build.VERSION_CODES.LOLLIPOP)  public static void jumpToGesturePassCheck(Activity activity) {    KeyguardManager keyguardManager =        (KeyguardManager) activity.getSystemService(Context.KEYGUARD_SERVICE);    Intent intent =        keyguardManager.createConfirmDeviceCredentialIntent("finger", "測試指紋識別");    activity.startActivityForResult(intent, REQUEST_CODE_CONFIRM_DEVICE_CREDENTIALS);  }  interface FingerPrintCallBack {    /**     * 識別成功     */    void onAuthenticationSucceeded();    /**     * 識別失敗     *     * @param count 還可以嘗試的次數     * @param count     */    void onAuthenticationFailed(int count);    /**     * 失敗次數過多     */    void onAuthenticationError();    /**     * 未開啟指紋功能     */    void FingerClosed();    /**     * 不支持指紋功能     */    void doNotSupportFinger();  }}

這個是工具類,下面上使用方法

package com.tsm.test;import android.app.Activity;import android.content.Intent;import android.os.Build;import android.os.Bundle;import android.util.Log;import android.view.View;import android.widget.Button;import android.widget.Toast;public class MainActivity extends Activity implements FingerPrintUiHelper.FingerPrintCallBack {  private FingerPrintUiHelper fingerPrintUiHelper;  @Override  protected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_main);    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {////指紋功能是23之后的版本才有的      initFingerPrint();      Button button = (Button) findViewById(R.id.button);      assert button != null;      button.setOnClickListener(new View.OnClickListener() {        @Override        public void onClick(View v) {          fingerPrintUiHelper.startFingerPrintListen();        }      });      findViewById(R.id.btn).setOnClickListener(new View.OnClickListener() {        @Override        public void onClick(View v) {          fingerPrintUiHelper.stopsFingerPrintListen();        }      });    }  }  private void initFingerPrint() {    fingerPrintUiHelper = new FingerPrintUiHelper(this, this);  }  @Override  protected void onActivityResult(int requestCode, int resultCode, Intent data) {    if (requestCode == FingerPrintUiHelper.REQUEST_CODE_CONFIRM_DEVICE_CREDENTIALS) {      // Challenge completed, proceed with using cipher      if (resultCode == RESULT_OK) {        Toast.makeText(this, "識別成功", Toast.LENGTH_SHORT).show();//        jumpToMain2Activity();      } else {        Toast.makeText(this, "識別失敗", Toast.LENGTH_SHORT).show();      }    }  }  @Override  protected void onDestroy() {    if (fingerPrintUiHelper != null)      fingerPrintUiHelper.stopsFingerPrintListen();    super.onDestroy();  }  /**   * 成功   */  @Override  public void onAuthenticationSucceeded() {    Toast.makeText(this, "識別成功", Toast.LENGTH_SHORT).show();  }  @Override  public void onAuthenticationFailed(int count) {    String msg = "您還可以嘗試%d次";    Toast.makeText(this, String.format(msg, count), Toast.LENGTH_SHORT).show();  }  /**   * 驗證失敗,走密碼驗證   */  @Override  public void onAuthenticationError() {    FingerPrintUiHelper.jumpToGesturePassCheck(this);  }   /**  * 沒有指紋功能  */  @Override  public void FingerClosed() {    //TODO 可以寫一個Dialog跳轉設置頁,這里我就不寫了    Toast.makeText(this, "指紋功能已關閉", Toast.LENGTH_SHORT).show();  }  @Override  public void doNotSupportFinger() {  Log.i("info", "-------------doNotSupportFinger--------------------");    Toast.makeText(this, "該設備不支持指紋功能", Toast.LENGTH_SHORT).show();  } }

最后添加權限:

<uses-permission android:name="android.permission.USE_FINGERPRINT" />

總結

以上所示是小編給大家介紹的Android 指紋功能實例代碼,希望對大家有所幫助,如果大家有任何疑問歡迎給我留言,小編會及時回復大家的!

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 石泉县| 吴堡县| 六枝特区| 武义县| 仁怀市| 巴中市| 五华县| 司法| 平果县| 台北县| 克什克腾旗| 古田县| 迁安市| 藁城市| 新乐市| 河池市| 繁昌县| 辛集市| 永顺县| 肇源县| 荣成市| 德令哈市| 丽水市| 通渭县| 平谷区| 连江县| 巴南区| 鄯善县| 仙游县| 温宿县| 元江| 会宁县| 九江县| 师宗县| 永寿县| 台山市| 乌恰县| 航空| 呼伦贝尔市| 宜宾市| 浦县|