Android

Android 指紋辨識

聲明權限

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

1.創建指紋辨識物件

FingerprintManagerCompat fingerManager = FingerprintManagerCompat.from(activity);

2.判斷硬體支持與註冊指紋

//判斷硬體是否支持
boolean isHardwareDetected = fingerManager.isHardwareDetected();
//判斷是否註冊指紋
boolean hasEnrolledFingerprints = fingerManager.hasEnrolledFingerprints();

3.調用認證callback

FingerprintManagerCompat.CryptoObject

這是一個加密類,在java中加密所用的類是Cipher,Signature,Mac

flags

這是一個optinal,這裡必須是0

CancellationSignal

這是一個信號類,直接new CancellationSignal(),可以取消指紋掃描等操作

FingerprintManagerCompat.AuthenticationCallback callback

指紋識別結果的監聽類,所有的结果處理都在這個類中

Handler handler

通常只是用來告知在哪個線程中返回结果,一般為null,默認會在主線程中回掉掃描結果

fingerManager.authenticate(null, 0, new CancellationSignal(), new FingerprintManagerCompat.AuthenticationCallback() {
    @Override
    public void onAuthenticationError(int errMsgId, CharSequence errString) {
           super.onAuthenticationError(errMsgId, errString);
           //指纹掃描出錯
    }

    @Override
    public void onAuthenticationHelp(int helpMsgId, CharSequence helpString) {
           super.onAuthenticationHelp(helpMsgId, helpString);
           //指纹掃描遇到可補救的錯誤提示
    }

     @Override
     public void onAuthenticationSucceeded(FingerprintManagerCompat.AuthenticationResult result) {
           super.onAuthenticationSucceeded(result);
           //指紋正確
     }

     @Override
     public void onAuthenticationFailed() {
           super.onAuthenticationFailed();
           //指纹不正確
     }
},null);

發表迴響