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

时间:2018-01-31 12:49:22

标签: android lockscreen securityexception android-fingerprint-api

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

java.lang.RuntimeException: 

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

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

at android.app.ActivityThread.access$1100 (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行是具有check fingerprintmanager.hasEnrolledFingerprints()

的检查

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

3 个答案:

答案 0 :(得分:3)

不确定您是否能够找到解决方案,作为解决方法,我只是将我们的电话包裹在权限检查中。

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

答案 1 :(得分:1)

对我有用的解决方案是:

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();
    }

我将检查放在嵌套if格式中,其中指纹认证仅在满足所有检查时启动。从那以后就没有这样的崩溃了。

答案 2 :(得分:0)

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

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

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