How to Use Unsupported Exception for Lower Platform Version

后端 未结 3 1199
渐次进展
渐次进展 2020-12-16 00:42

I have a DialogFragment that handles login and fingerprint authentication for my application. This fragment uses two classes that are exclusive to API 23,

相关标签:
3条回答
  • 2020-12-16 01:05

    As you said the problem is with that catch block

    catch (KeyPermanentlyInvalidatedException exception) {
        Timber.w(exception, "A new fingerprint was added to the device");
        handleKeyPermanentlyInvalidated();
        return false;
    }
    

    Because that exception is added on API LEVEL 23, but I don't know why the verify error is thrown at initialization time itself.

    Anyway you can catch the exception using

    catch (InvalidKeyExceptionexception) {
        ....
        return false;
    }
    

    since KeyPermanentlyInvalidatedException extends InvalidKeyExceptionexception

    0 讨论(0)
  • 2020-12-16 01:09

    I had the same error and solved it the following way:

    catch (Exception e) {
        if (e instanceof KeyPermanentlyInvalidatedException) {
            //your error handling goes here
        }
    

    It isn't vey nice, but it works

    0 讨论(0)
  • 2020-12-16 01:14

    My guess is that either FingerprintCallback.Callback extends an API Level 23+ interface or that LoginFragment has fields that reference API Level 23+ stuff.

    Your rule about being able to call API Level 23+ methods safely inside the version guard block is correct. However, you cannot:

    • inherit from classes that do not exist on the device
    • implement interfaces that do not exist on the device
    • have fields whose types do not exist on the device
    • accept constructor or method parameters whose types do not exist on the device (where we actually call these)
    • have method return values whose types do not exist on the device (where we actually call these)

    In many cases, we don't need any of that, in which case just checking Build.VERSION.SDK_INT before calling API Level 23+ methods is sufficient.

    If you need to do some of the things in the bulleted list, that's fine, but then you need to isolate those into classes that you only use on API Level 23+ devices.

    So, for example, let's pretend that the problem is that FingerprintCallback.Callback extends some API Level 23+ interface. Rather than implementing FingerprintCallback.Callback on the LoginFragment, you might implement that as an anonymous inner class, and only execute the code creating that anonymous inner class instance if Build.VERSION.SDK_INT is high enough. Then, you only are referencing FingerprintCallback.Callback on the newer devices, and you should be safe.

    0 讨论(0)
提交回复
热议问题