Android 指紋功能實(shí)例代碼
最近在做項(xiàng)目的時(shí)候遇到了添加打開(kāi)app圖像解鎖的功能,自己嘴欠說(shuō)現(xiàn)在都用指紋功能,自己給自己挖了一個(gè)坑,真是沒(méi)誰(shuí)了
從網(wǎng)上看了一些資料,但是給我demo考慮的不是很多,設(shè)備支不支持都沒(méi)考慮,如果支持的話是否添加過(guò)指紋也不知道,其實(shí)方法都很簡(jiǎn)單。
廢話不多說(shuō),貼上工具類和使用方法
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ù)過(guò)多,調(diào)用系統(tǒng)的startActivityForResult * 這個(gè)是code */ public static final int REQUEST_CODE_CONFIRM_DEVICE_CREDENTIALS = 10; /** * 用于提示用戶還可以嘗試幾次,比較友好 */ private int count; /** * 控制是否開(kāi)啟過(guò)指紋功能 */ 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(); } } /** * 開(kāi)始掃描指紋 */ 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", "測(cè)試指紋識(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ù)過(guò)多 */ void onAuthenticationError(); /** * 未開(kāi)啟指紋功能 */ 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); } /** * 沒(méi)有指紋功能 */ @Override public void FingerClosed() { //TODO 可以寫(xiě)一個(gè)Dialog跳轉(zhuǎn)設(shè)置頁(yè),這里我就不寫(xiě)了 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ì)大家有所幫助,如果大家有任何疑問(wèn)歡迎給我留言,小編會(huì)及時(shí)回復(fù)大家的!
相關(guān)文章
Android 簡(jiǎn)單的實(shí)現(xiàn)滑塊拼圖驗(yàn)證碼功能
這篇文章主要介紹了Android 簡(jiǎn)單的實(shí)現(xiàn)滑塊拼圖驗(yàn)證碼功能,幫助大家更好的理解和學(xué)習(xí)使用Android開(kāi)發(fā),感興趣的朋友可以了解下2021-03-03Android編程實(shí)現(xiàn)帶有圖標(biāo)的ListView并帶有長(zhǎng)按菜單效果示例
這篇文章主要介紹了Android編程實(shí)現(xiàn)帶有圖標(biāo)的ListView并帶有長(zhǎng)按菜單效果,結(jié)合實(shí)例形式分析了Android帶圖標(biāo)的ListView及菜單功能相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2017-06-06android view實(shí)現(xiàn)橫向滑動(dòng)選擇
這篇文章主要為大家詳細(xì)介紹了android view實(shí)現(xiàn)橫向滑動(dòng)選擇,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-07-07Android開(kāi)發(fā)中4個(gè)常用的工具類【Toast、SharedPreferences、網(wǎng)絡(luò)及屏幕操作】
這篇文章主要介紹了Android開(kāi)發(fā)中4個(gè)常用的工具類,包括Toast管理、SharedPreferences存儲(chǔ)管理、網(wǎng)絡(luò)操作及屏幕操作等功能的封裝類,需要的朋友可以參考下2017-11-11Android實(shí)現(xiàn)環(huán)信修改頭像和昵稱
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)環(huán)信修改頭像和昵稱,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-02-02Android自定義動(dòng)態(tài)壁紙開(kāi)發(fā)(時(shí)鐘)
今天小編就為大家分享一篇關(guān)于Android自定義動(dòng)態(tài)壁紙開(kāi)發(fā)(時(shí)鐘),小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2019-01-01

基于adbkit的android設(shè)備管理(精簡(jiǎn)版stf)