Android 指紋功能實(shí)例代碼
最近在做項(xiàng)目的時(shí)候遇到了添加打開app圖像解鎖的功能,自己嘴欠說現(xiàn)在都用指紋功能,自己給自己挖了一個(gè)坑,真是沒誰了
從網(wǎng)上看了一些資料,但是給我demo考慮的不是很多,設(shè)備支不支持都沒考慮,如果支持的話是否添加過指紋也不知道,其實(shí)方法都很簡單。
廢話不多說,貼上工具類和使用方法
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/>
* 指紋識(shí)別功能
*
* 如果創(chuàng)建了該類的實(shí)例,必須調(diào)用 stopsFingerPrintListen 方法
*
* 添加權(quán)限
* <uses-permission android:name="android.permission.USE_FINGERPRINT" />
*
*/
public class FingerPrintUiHelper extends FingerprintManagerCompat.AuthenticationCallback {
private final FingerPrintCallBack callback;
private CancellationSignal signal;
private FingerprintManagerCompat fingerprintManager;
/**
* 如果失敗次數(shù)過多,調(diào)用系統(tǒng)的startActivityForResult
* 這個(gè)是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);
}
/**
* 初始化未必調(diào)用 startFingerPrintListen
* 所以添加變量控制
*/
public void stopsFingerPrintListen() {
if (isStartFinger) {
if (signal != null && !signal.isCanceled()) {
signal.cancel();
}
}
}
/**
* 識(shí)別成功
* @param result
*/
@Override
public void onAuthenticationSucceeded(FingerprintManagerCompat.AuthenticationResult result) {
if (callback != null)
callback.onAuthenticationSucceeded();
}
/**
* 識(shí)別失敗
*/
@Override
public void onAuthenticationFailed() {
count--;
if (count > 0) {
if (callback != null)
callback.onAuthenticationFailed(count);
return;
}
}
/**
* 有錯(cuò)誤
* @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;
}
}
/**
* 多次調(diào)用指紋識(shí)別失敗后,調(diào)用此方法
*
* @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", "測試指紋識(shí)別");
activity.startActivityForResult(intent, REQUEST_CODE_CONFIRM_DEVICE_CREDENTIALS);
}
interface FingerPrintCallBack {
/**
* 識(shí)別成功
*/
void onAuthenticationSucceeded();
/**
* 識(shí)別失敗
*
* @param count 還可以嘗試的次數(shù)
* @param count
*/
void onAuthenticationFailed(int count);
/**
* 失敗次數(shù)過多
*/
void onAuthenticationError();
/**
* 未開啟指紋功能
*/
void FingerClosed();
/**
* 不支持指紋功能
*/
void doNotSupportFinger();
}
}
這個(gè)是工具類,下面上使用方法
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, "識(shí)別成功", Toast.LENGTH_SHORT).show();
// jumpToMain2Activity();
} else {
Toast.makeText(this, "識(shí)別失敗", Toast.LENGTH_SHORT).show();
}
}
}
@Override
protected void onDestroy() {
if (fingerPrintUiHelper != null)
fingerPrintUiHelper.stopsFingerPrintListen();
super.onDestroy();
}
/**
* 成功
*/
@Override
public void onAuthenticationSucceeded() {
Toast.makeText(this, "識(shí)別成功", Toast.LENGTH_SHORT).show();
}
@Override
public void onAuthenticationFailed(int count) {
String msg = "您還可以嘗試%d次";
Toast.makeText(this, String.format(msg, count), Toast.LENGTH_SHORT).show();
}
/**
* 驗(yàn)證失敗,走密碼驗(yàn)證
*/
@Override
public void onAuthenticationError() {
FingerPrintUiHelper.jumpToGesturePassCheck(this);
}
/**
* 沒有指紋功能
*/
@Override
public void FingerClosed() {
//TODO 可以寫一個(gè)Dialog跳轉(zhuǎn)設(shè)置頁,這里我就不寫了
Toast.makeText(this, "指紋功能已關(guān)閉", Toast.LENGTH_SHORT).show();
}
@Override
public void doNotSupportFinger() {
Log.i("info", "-------------doNotSupportFinger--------------------");
Toast.makeText(this, "該設(shè)備不支持指紋功能", Toast.LENGTH_SHORT).show();
}
}
最后添加權(quán)限:
<uses-permission android:name="android.permission.USE_FINGERPRINT" />
總結(jié)
以上所示是小編給大家介紹的Android 指紋功能實(shí)例代碼,希望對(duì)大家有所幫助,如果大家有任何疑問歡迎給我留言,小編會(huì)及時(shí)回復(fù)大家的!
相關(guān)文章
Android之ArcSlidingHelper制作圓弧滑動(dòng)效果
這篇文章主要介紹了Android之ArcSlidingHelper制作圓弧滑動(dòng)效果,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-08-08
Android中極簡的js與java的交互庫(SimpleJavaJsBridge)
本文主要介紹了Android中極簡的js與java的交互庫--SimpleJavaJsBridge,它可以讓js與java之間的通信更簡單。 具有很好的參考價(jià)值,下面跟著小編一起來看下吧2017-01-01
Android自定義View實(shí)現(xiàn)簡單水波紋效果
這篇文章主要為大家詳細(xì)介紹了Android自定義View實(shí)現(xiàn)簡單水波紋效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-08-08
Android用戶輸入自動(dòng)提示控件AutoCompleteTextView使用方法
這篇文章主要為大家詳細(xì)介紹了Android用戶輸入自動(dòng)提示控件AutoCompleteTextView的使用方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08
基于Flutter實(shí)現(xiàn)愛心三連動(dòng)畫效果
Animation是一個(gè)抽象類,它并不參與屏幕的繪制,而是在設(shè)定的時(shí)間范圍內(nèi)對(duì)一段區(qū)間值進(jìn)行插值。本文將利用Animation制作一個(gè)愛心三連動(dòng)畫效果,感興趣的可以學(xué)習(xí)一下2022-03-03
Android 用戶Session管理的設(shè)計(jì)方案
這篇文章主要介紹了Android 用戶Session管理的設(shè)計(jì)方案,需要的朋友可以參考下2017-12-12
Android隱藏標(biāo)題欄及解決啟動(dòng)閃過標(biāo)題的實(shí)例詳解
這篇文章主要介紹了Android隱藏標(biāo)題欄及解決啟動(dòng)閃過標(biāo)題的實(shí)例詳解的相關(guān)資料,這里提供兩種方法幫助大家解決這種問題,需要的朋友可以參考下2017-09-09

