您好,登錄后才能下訂單哦!
本篇文章為大家展示了Android中怎么實現指紋功能,內容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細介紹希望你能有所收獲。
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中怎么實現指紋功能,你們學到知識或技能了嗎?如果還想學到更多技能或者豐富自己的知識儲備,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。