Android 指紋識別,指紋支付demo

meixi_android&java發表於2018-08-30

demo連結:https://download.csdn.net/download/meixi_android/10796468

1、指紋工具類:

/**
 * 作者:created by meixi
 * 郵箱:13164716840@163.com
 * 日期:2018/8/30 09
 */

public class FingerprintManagerUtil {
    private static FingerprintManagerCompat fingerprintManagerCompat;
    private static CancellationSignal cancellationSignal;

    private FingerprintManagerUtil() {
    }

    /**
     * 開始進行指紋驗證
     *
     * @param context                    上下文
     * @param fingerprintListener 指紋驗證回撥介面
     */
    public static void startFingerprinterVerification(Context context, final FingerprintListener fingerprintListener) {
        fingerprintManagerCompat = FingerprintManagerCompat.from(context);

        // 是否支援指紋驗證
        if (fingerprintManagerCompat == null || !fingerprintManagerCompat.isHardwareDetected()) {
            if (fingerprintListener != null)
                fingerprintListener.onNonsupport();
            return;
        }

        // 是否錄入了指紋
        if (!fingerprintManagerCompat.hasEnrolledFingerprints()) {
            if (fingerprintListener != null)
                fingerprintListener.onEnrollFailed();
            return;
        }

        // 回撥可以開始進行認證
        if (fingerprintListener != null)
            fingerprintListener.onAuthenticationStart();

        cancellationSignal = new CancellationSignal();
        fingerprintManagerCompat.authenticate(null, 0, cancellationSignal, new FingerprintManagerCompat.AuthenticationCallback() {
            @Override
            public void onAuthenticationError(int errMsgId, CharSequence errString) {
                // 驗證出錯回撥,指紋感測器會關閉一段時間
                super.onAuthenticationError(errMsgId, errString);
                if (fingerprintListener != null)
                    fingerprintListener.onAuthenticationError(errMsgId, errString);
            }

            @Override
            public void onAuthenticationHelp(int helpMsgId, CharSequence helpString) {
                // 驗證幫助回撥
                super.onAuthenticationHelp(helpMsgId, helpString);
                if (fingerprintListener != null)
                    fingerprintListener.onAuthenticationHelp(helpMsgId, helpString);
            }

            @Override
            public void onAuthenticationSucceeded(FingerprintManagerCompat.AuthenticationResult result) {
                // 驗證成功
                super.onAuthenticationSucceeded(result);
                if (fingerprintListener != null)
                    fingerprintListener.onAuthenticationSucceeded(result);
            }

            @Override
            public void onAuthenticationFailed() {
                // 驗證失敗  指紋驗證失敗後,指紋感測器不會立即關閉指紋驗證,
                // 系統會提供5次重試的機會,即呼叫5次onAuthenticationFailed()後,才會呼叫onAuthenticationError()
                super.onAuthenticationFailed();
                if (fingerprintListener != null)
                    fingerprintListener.onAuthenticationFailed();
            }
        }, null);
    }

    /**
     * 取消指紋驗證
     */
    public static void cancel() {
        if (cancellationSignal != null && !cancellationSignal.isCanceled())
            cancellationSignal.cancel();
    }

    /**
     * 指紋驗證回撥監聽
     */
    public interface FingerprintListener {
        /**
         * 手機或系統不支援指紋驗證時回撥
         */
        void onNonsupport();

        /**
         * 手機支援指紋驗證,但是還沒有錄入指紋時回撥
         */
        void onEnrollFailed();

        /**
         * 可以進行指紋驗證時回撥,該方法主要作用用於在進行指紋驗證之前做一些操作,比如彈出對話方塊
         */
        void onAuthenticationStart();

        /**
         * 指紋驗證成功時回撥
         *
         * @param result {@link FingerprintManagerCompat.AuthenticationResult} 物件
         */
        void onAuthenticationSucceeded(FingerprintManagerCompat.AuthenticationResult result);

        /**
         * 驗證失敗時回撥
         * <br/>&nbsp;&nbsp;&nbsp;&nbsp;
         * 指紋驗證失敗後,指紋感測器不會立即關閉指紋驗證,系統會提供5次重試的機會,即呼叫
         * 5次 {@link #onAuthenticationFailed()} 後,才會呼叫 {@link #onAuthenticationError(int, CharSequence)}
         */
        void onAuthenticationFailed();

        /**
         * 驗證出錯時回撥,指紋感測器會關閉一段時間,具體時間根據廠商不同有所區別
         *
         * @param errMsgId  錯誤資訊id
         * @param errString 錯誤資訊描述
         */
        void onAuthenticationError(int errMsgId, CharSequence errString);

        /**
         * 驗證幫助回撥
         *
         * @param helpMsgId  幫助資訊id
         * @param helpString 幫助資訊描述
         */
        void onAuthenticationHelp(int helpMsgId, CharSequence helpString);
    }

    /**
     * FingerprintListener 回撥監聽介面卡,減少不必要方法的重寫,只需要重寫需要處理的對應方法即可
     */
    public abstract static class FingerprintListenerAdapter implements FingerprintListener {

        @Override
        public void onNonsupport() {
        }

        @Override
        public void onEnrollFailed() {
        }

        @Override
        public void onAuthenticationStart() {
        }

        @Override
        public void onAuthenticationFailed() {
        }

        @Override
        public void onAuthenticationError(int errMsgId, CharSequence errString) {
        }

        @Override
        public void onAuthenticationHelp(int helpMsgId, CharSequence helpString) {
        }
    }
}

2、activity呼叫以及指紋識別驗證回撥方法(呼叫後,觸控手機指紋按鈕即可實現指紋識別)

private AlertDialog alertDialog;
// 呼叫指紋驗證方法
private void startVerification() {
    FingerprintManagerUtil.startFingerprinterVerification(this,
            new FingerprintManagerUtil.FingerprintListenerAdapter() {

                @Override
                public void onAuthenticationStart() {
                    AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this)
                            .setTitle("指紋驗證")
                            .setMessage("指紋驗證測試")
                            .setCancelable(false)
                            .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    FingerprintManagerUtil.cancel();
                                }
                            });
                    alertDialog = builder.create();
                    alertDialog.show();
                }

                @Override
                public void onNonsupport() {
                    Log.i("MainActivity", "onNonsupport");
                    Toast.makeText(MainActivity.this, "不支援指紋驗證", Toast.LENGTH_SHORT).show();
                }

                @Override
                public void onEnrollFailed() {
                    Log.i("MainActivity", "onEnrollFailed");
                    Toast.makeText(MainActivity.this, "沒有錄入指紋", Toast.LENGTH_SHORT).show();
                }

                @Override
                public void onAuthenticationSucceeded(FingerprintManagerCompat.AuthenticationResult result) {
                    alertDialog.dismiss();
                    Log.i("MainActivity", "onAuthenticationSucceeded result = [" + result + "]");
                    Toast.makeText(MainActivity.this, "驗證成功", Toast.LENGTH_SHORT).show();
                }

                @Override
                public void onAuthenticationFailed() {
                    Log.i("MainActivity", "onAuthenticationFailed");
                    Toast.makeText(MainActivity.this, "驗證失敗", Toast.LENGTH_SHORT).show();
                }

                @Override
                public void onAuthenticationError(int errMsgId, CharSequence errString) {
                    Log.i("MainActivity", "onAuthenticationError errMsgId = [" + errMsgId + "], errString = [" + errString + "]");
                    Toast.makeText(MainActivity.this, "提示: " + errString, Toast.LENGTH_SHORT).show();
                }

                @Override
                public void onAuthenticationHelp(int helpMsgId, CharSequence helpString) {
                    Log.i("MainActivity", "onAuthenticationHelp helpMsgId = [" + helpMsgId + "], helpString = [" + helpString + "]");
                    Toast.makeText(MainActivity.this, "提示: " + helpString, Toast.LENGTH_SHORT).show();
                }
            });
}

需要指紋識別許可權:

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

相關文章