Skip to content Skip to sidebar Skip to footer

I Keep Getting Deprecated Api Warning Even With Correct Check

I'm working on a Android project, and at some point in my code I need to get the device serial number. In order to get it I used to use Build.SERIAL which has been deprecated since

Solution 1:

You need something like this:

@SuppressLint("HardwareIds")
@SuppressWarnings("deprecation")
private static String getSerial() throws SecurityException {
    if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.O) {
        returnBuild.SERIAL;
    }
    returnBuild.getSerial();
}

to hide all the incorrect warnings

Solution 2:

It's annoying but I've only found something like this to work:

@TargetApi(BUILD.VERSION_CODES.N)privatestatic String getDeprecatedSerial() {
    return BUILD.SERIAL;
}

privatestatic String getSerial()throws SecurityException {
    if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.O) {
        return getDeprecatedSerial();
    }

    return Build.getSerial();
}

This way you don't get deprecation warnings about the API level for this API & you don't suppress all deprecation warnings.

Post a Comment for "I Keep Getting Deprecated Api Warning Even With Correct Check"