7

我正在尝试实现指纹以通过指纹身份验证解锁或删除锁定的视图,使用现有的已注册指纹通过传感器进一步显示内容,但指纹身份验证在正常活动中工作正常但在自定义视图中不起作用,我也尝试实现它在我的自定义屏幕锁定应用程序中,但它存在冲突或可能是由于资源不可用面临此错误“FINGERPRINT_ERROR_CANCELED”,错误代码为 5

我不确定它是否是由于资源不可用,因为同时内置的安全密码/图案锁也使用相同的指纹传感器以及我自己的屏幕锁或自定义视图

此代码在活动中运行良好

 fingerprintHandler = new FingerprintHandler();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {

            keyguardManager =
                    (KeyguardManager) getSystemService(KEYGUARD_SERVICE);
            mFingerprintManager =
                    (FingerprintManager) getSystemService(FINGERPRINT_SERVICE);

//
//            generateKey();
//            if (cipherInit()) {
//                cryptoObject =
//                        new FingerprintManager.CryptoObject(cipher);
            fingerprintHandler.setOnAuthenticationListener(new FingerprintManager.AuthenticationCallback() {
                @RequiresApi(api = Build.VERSION_CODES.M)
                @Override
                public void onAuthenticationError(int errorCode, CharSequence errString) {
                    Toast.makeText(FingerprintNormalActivity.this,
                            "Authentication error\n" + "Error code" + errorCode + "\nError String" + errString,
                            Toast.LENGTH_LONG).show();
                    imageViewSmokeImages.setImageResource(R.drawable.fingerprinterror);
                    tvstatus.setText("Authentication error\n" + "Error code" + errorCode + "\nError String" + errString);
                }

                @RequiresApi(api = Build.VERSION_CODES.M)
                @Override
                public void onAuthenticationHelp(int helpCode, CharSequence helpString) {`enter code here`
                    Toast.makeText(FingerprintNormalActivity.this,
                            "Authentication help\n" + helpString,
                            Toast.LENGTH_LONG).show();
                    imageViewSmokeImages.setImageResource(R.drawable.help);
                    tvstatus.setText("Authentication help\n" + helpString);

                }

                @Override
                public void onAuthenticationSucceeded(FingerprintManager.AuthenticationResult result) {
                    Toast.makeText(FingerprintNormalActivity.this,
                            "Authentication succeeded.",
                            Toast.LENGTH_LONG).show();
                    imageViewSmokeImages.setImageResource(R.drawable.fingerprintsuccess);
                    tvstatus.setText("Authentication Successfull");

                    finish();
                }

                @RequiresApi(api = Build.VERSION_CODES.M)
                @Override
                public void onAuthenticationFailed() {
                    Toast.makeText(FingerprintNormalActivity.this,
                            "Authentication failed.",
                            Toast.LENGTH_LONG).show();
                    tvstatus.setText("Authentication failed");

                    imageViewSmokeImages.setImageResource(R.drawable.fingerprintfailed);


                }
            });
//            new AuthenticateUser();

////                fingerprintHandler.startListening();
            if (!getKeyStore())
                return;

            if (!createNewKey(false)) {
                return;
            }
            if (!getCipher()) {
                return;
            }
            if (!initCipher(Cipher.ENCRYPT_MODE)) {
                return;
            }
            if (!initCryptObject()) {
                return;
            } else {
                fingerprintHandler.startListening(cryptoObject);
            }
//            }
        }

这是我要听的指纹句柄课

private static FingerprintManager.AuthenticationCallback mAuthenticationCallback;
    private static CancellationSignal mCancellationSignal;
    private static Context mContext;

    @RequiresApi(api = Build.VERSION_CODES.M)
    public class FingerprintHandler {

        public void setOnAuthenticationListener(FingerprintManager.AuthenticationCallback listener) {
            mAuthenticationCallback = listener;
        }

        public void startListening() {
            if (isFingerScannerAvailableAndSet()) {
                try {
                    mCancellationSignal = new CancellationSignal();
                    mFingerprintManager.authenticate(null, mCancellationSignal, 0 /* flags */, mAuthenticationCallback, new Handler(Looper.getMainLooper()));
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }

        public void startListening(FingerprintManager.CryptoObject cryptoObject) {
            if (isFingerScannerAvailableAndSet()) {
                try {
                    mCancellationSignal = new CancellationSignal();
                    mFingerprintManager.authenticate(cryptoObject, mCancellationSignal, 0 /* flags */, mAuthenticationCallback, new Handler(Looper.getMainLooper()));
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }

        public void stopListening() {
            if (isFingerScannerAvailableAndSet()) {
                try {
                    mCancellationSignal.cancel();
                    mCancellationSignal = null;
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }

        public boolean isFingerScannerAvailableAndSet() {
            if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M)
                return false;
            if (ActivityCompat.checkSelfPermission(FingerprintNormalActivity.this, Manifest.permission.USE_FINGERPRINT) != PackageManager.PERMISSION_GRANTED) {
                Toast.makeText(FingerprintNormalActivity.this, "User hasn't granted permission to use Fingerprint", Toast.LENGTH_LONG).show();
                return false;
            }
            if (mFingerprintManager == null){
                Toast.makeText(FingerprintNormalActivity.this,
                        "mFingerprintManager is null",
                        Toast.LENGTH_LONG).show();
                tvstatus.setText("mFingerprintManager is null");
                return false;
            }
            if (!mFingerprintManager.isHardwareDetected()) {
                Toast.makeText(FingerprintNormalActivity.this,
                        "fingerprint hardware not present or not functional",
                        Toast.LENGTH_LONG).show();
                tvstatus.setText("fingerprint hardware not present or not functional");
                return false;
            }
            if (!mFingerprintManager.hasEnrolledFingerprints()) {
                Toast.makeText(FingerprintNormalActivity.this,
                        "no fingerprint enrolled/saved",
                        Toast.LENGTH_LONG).show();
                tvstatus.setText("no fingerprint enrolled/saved");
                return false;
            }
            return true;
        }
    }
4

1 回答 1

5

看到这个问题。这似乎发生在大多数设备上,需要作为特殊情况处理。似乎对我有用的是在此错误上停止指纹侦听器,然后再次重新启动它。

@Override
    public void onAuthenticationError(int errMsgId, CharSequence errString)            {
     if (errMsgId == FingerprintManager.FINGERPRINT_ERROR_CANCELED){
         stopListening();
         restartListeningToFingerprint();
     }
}

在 restartListening() 方法中,我只是调用startListening一个新的 Cipher 实例。这似乎适用于我拥有的所有设备。但我确实看到了一些随机的致命异常,一些三星设备可能是这种情况的副作用。我很想知道其他人是如何处理这个错误的。

于 2017-03-22T06:07:21.130 回答