检查指纹是否已在三星手机中注册时出现 SecurityException

SecurityException while checking if fingerprints are enrolled in Samsung Phones

我在我的应用程序中使用带指纹的锁屏。虽然它可以与其他具有指纹传感器的手机无缝协作,但三星用户正面临一些 SecurityException,正如我在 google 控制台中看到的那样 reports.Here 是报告:

java.lang.RuntimeException: 

at android.app.ActivityThread.performLaunchActivity (ActivityThread.java:3319)

at android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:3415)

at android.app.ActivityThread.access00 (ActivityThread.java:229)

at android.app.ActivityThread$H.handleMessage (ActivityThread.java:1821)

at android.os.Handler.dispatchMessage (Handler.java:102)

at android.os.Looper.loop (Looper.java:148)

 at android.app.ActivityThread.main (ActivityThread.java:7406)

at java.lang.reflect.Method.invoke (Native Method)

at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:1230)

at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1120)

Caused by: java.lang.SecurityException: 

at android.os.Parcel.readException (Parcel.java:1621)

at android.os.Parcel.readException (Parcel.java:1574)

at android.hardware.fingerprint.IFingerprintService$Stub$Proxy.hasEnrolledFingerprints (IFingerprintService.java:503)

at android.hardware.fingerprint.FingerprintManager.hasEnrolledFingerprints (FingerprintManager.java:776)

at com.example.ark.access.LockScreen.setUpFingerPrint (LockScreen.java:252)

at com.example.ark.access.LockScreen.onCreate (LockScreen.java:67)

at android.app.Activity.performCreate (Activity.java:6904)

at android.app.Instrumentation.callActivityOnCreate (Instrumentation.java:1136)

at android.app.ActivityThread.performLaunchActivity (ActivityThread.java:3266)

这是我的文件中检查指纹的部分:

    private void setUpFingerPrint(ImageView white,ImageView black)
{
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        //Get an instance of KeyguardManager and FingerprintManager//
        KeyguardManager keyguardManager = (KeyguardManager) getSystemService(KEYGUARD_SERVICE);
        FingerprintManager fingerprintManager = (FingerprintManager) getSystemService(FINGERPRINT_SERVICE);


        //Check whether the device has a fingerprint sensor//
        if (!fingerprintManager.isHardwareDetected()) {
            // If a fingerprint sensor isn’t available, then inform the user that they’ll be unable to use your app’s fingerprint functionality//
            //Toast.makeText(this, R.string.noFingerPrint, Toast.LENGTH_SHORT).show();
            white.setVisibility(View.GONE);
            black.setVisibility(View.GONE);
        }
        //Check whether the user has granted your app the USE_FINGERPRINT permission//
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.USE_FINGERPRINT) != PackageManager.PERMISSION_GRANTED) {
            // If your app doesn't have this permission, then display the following text//
            Toast.makeText(this, R.string.noFingerPrintPermission, Toast.LENGTH_SHORT).show();
        }

        //Check that the user has registered at least one fingerprint//
        if (!fingerprintManager.hasEnrolledFingerprints()) {
            // If the user hasn’t configured any fingerprints, then display the following message//
            Toast.makeText(this, R.string.noFingerPrintRegistered, Toast.LENGTH_SHORT).show();
        }

        //Check that the lockscreen is secured//
        if (!keyguardManager.isKeyguardSecure()) {
            // If the user hasn’t secured their lockscreen with a PIN password or pattern, then display the following text//
            Toast.makeText(this, R.string.lockScreenNotConfigured, Toast.LENGTH_SHORT).show();
        }
        else {
            try {
                generateKey();
            } catch (FingerprintException e) {
                e.printStackTrace();
            }

            if (initCipher()) {
                //If the cipher is initialized successfully, then create a CryptoObject instance//
                FingerprintManager.CryptoObject cryptoObject = new FingerprintManager.CryptoObject(cipher);

                // Here, I’m referencing the FingerprintHandler class that we’ll create in the next section. This class will be responsible
                // for starting the authentication process (via the startAuth method) and processing the authentication process events//
                int k = getIntent().getIntExtra("Mode", 0);
                FingerprintHandler helper = new FingerprintHandler(this,k,white,black);
                helper.startAuth(fingerprintManager, cryptoObject);
            }
        }
    }
    else
    {
        white.setVisibility(View.GONE);
        black.setVisibility(View.GONE);
    }
}

第 252 行是检查 fingerprintmanager.hasEnrolledFingerprints()

我很难搞清楚,因为我没有三星手机可以测试。到目前为止,它已经发生在 Galaxy J7 和 Grand Prime Plus 中。

您是否请求访问应用程序清单中的指纹 API 的权限?

您必须将以下行插入应用的权限部分。

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

不确定您是否曾为此找到解决方案,作为解决方法,我只是将我们的调用包装在权限检查中。

inline val Activity.fingerprintManager: FingerprintManagerCompat?
  get() = (
    if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.USE_FINGERPRINT) == PackageManager.PERMISSION_GRANTED) {
      FingerprintManagerCompat.from(this)
    } else { null }
  )

对我有用的解决方案是:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        //Get an instance of KeyguardManager and FingerprintManager//
        KeyguardManager keyguardManager = (KeyguardManager) context.getSystemService(KEYGUARD_SERVICE);
        FingerprintManager fingerprintManager = (FingerprintManager)context.getSystemService(FINGERPRINT_SERVICE);


        //Check whether the device has a fingerprint sensor//
        if (!fingerprintManager.isHardwareDetected()) {
            // If a fingerprint sensor isn’t available, then inform the user that they’ll be unable to use your app’s fingerprint functionality//
            //Toast.makeText(this, R.string.noFingerPrint, Toast.LENGTH_SHORT).show();
            listener.noFingerPrintHardware();
        } else {
            //Check whether the user has granted your app the USE_FINGERPRINT permission//
            if (ActivityCompat.checkSelfPermission(context, Manifest.permission.USE_FINGERPRINT) != PackageManager.PERMISSION_GRANTED) {
                listener.fingerPrintPermissionError();
            } else {
                //Check that the user has registered at least one fingerprint//
                if (!fingerprintManager.hasEnrolledFingerprints()) {
                    listener.noEnrolledFingerprints();
                } else {

                    //Check that the lockscreen is secured//
                    if (!keyguardManager.isKeyguardSecure()) {
                        listener.keygaurdNotSecure();
                    } else {
                        try {
                            generateKey();
                        } catch (FingerprintException e) {
                            e.printStackTrace();
                        }

                        if (initCipher(listener)) {
                            //If the cipher is initialized successfully, then create a CryptoObject instance//
                            FingerprintManager.CryptoObject cryptoObject = new FingerprintManager.CryptoObject(cipher);

                            // Here, I’m referencing the FingerprintHandler class that we’ll create in the next section. This class will be responsible
                            // for starting the authentication process (via the startAuth method) and processing the authentication process events//
                            FingerprintHandler helper = new FingerprintHandler(context,listener);
                            helper.startAuth(fingerprintManager, cryptoObject);
                        }
                    }
                }
            }
        }
    }
    else
    {
        listener.noFingerPrintHardware();
    }

我将检查设置为 nested-if 格式,其中指纹认证仅在满足所有检查后才开始。从那以后就没有发生过这样的崩溃。