本篇文章為大家展示了Android中怎么實現(xiàn)指紋功能,內(nèi)容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細(xì)介紹希望你能有所收獲。
創(chuàng)新互聯(lián)公司服務(wù)項目包括新蔡網(wǎng)站建設(shè)、新蔡網(wǎng)站制作、新蔡網(wǎng)頁制作以及新蔡網(wǎng)絡(luò)營銷策劃等。多年來,我們專注于互聯(lián)網(wǎng)行業(yè),利用自身積累的技術(shù)優(yōu)勢、行業(yè)經(jīng)驗、深度合作伙伴關(guān)系等,向廣大中小型企業(yè)、政府機(jī)構(gòu)等提供互聯(lián)網(wǎng)行業(yè)的解決方案,新蔡網(wǎng)站推廣取得了明顯的社會效益與經(jīng)濟(jì)效益。目前,我們服務(wù)的客戶以成都為中心已經(jīng)輻射到新蔡省份的部分城市,未來相信會繼續(xù)擴(kuò)大服務(wù)區(qū)域并繼續(xù)獲得客戶的支持與信任!
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. * * 指紋識別功能 * * 如果創(chuàng)建了該類的實例,必須調(diào)用 stopsFingerPrintListen 方法 * * 添加權(quán)限 ** */ public class FingerPrintUiHelper extends FingerprintManagerCompat.AuthenticationCallback { private final FingerPrintCallBack callback; private CancellationSignal signal; private FingerprintManagerCompat fingerprintManager; /** * 如果失敗次數(shù)過多,調(diào)用系統(tǒng)的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); } /** * 初始化未必調(diào)用 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; } } /** * 多次調(diào)用指紋識別失敗后,調(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", "測試指紋識別"); activity.startActivityForResult(intent, REQUEST_CODE_CONFIRM_DEVICE_CREDENTIALS); } interface FingerPrintCallBack { /** * 識別成功 */ void onAuthenticationSucceeded(); /** * 識別失敗 * * @param count 還可以嘗試的次數(shù) * @param count */ void onAuthenticationFailed(int count); /** * 失敗次數(shù)過多 */ 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跳轉(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)限:
上述內(nèi)容就是Android中怎么實現(xiàn)指紋功能,你們學(xué)到知識或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識儲備,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。