即使进行了正确的检查,我仍然不赞成使用API​​警告 [英] I keep getting deprecated API warning even with correct check

查看:78
本文介绍了即使进行了正确的检查,我仍然不赞成使用API​​警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个Android项目,在代码中的某个点上,我需要获取设备序列号。

I'm working on a Android project, and at some point in my code I need to get the device serial number.

为了获得它,我使用了使用 Build.SERIAL (自Android O 开始不推荐使用)。为了避免出现问题,我开始使用 Build.getSerial(),并创建了一个包装操作系统版本检查的小方法:

In order to get it I used to use Build.SERIAL which has been deprecated since Android O. To avoid problems I started using Build.getSerial(), and created a little method that wraps up the OS version check:

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

    return Build.getSerial();
}

注意:我不是在检查权限 READ_PHONE_STATE getSerial()方法要求),因为我从一开始就在做,并确保已经拥有<

Note: I'm not checking for the permission READ_PHONE_STATE (required by the getSerial() method) to be granted because I'm doing it at the start and make sure I already have it before getting to this method.

问题是,无论我如何写下Android OS检查,我都会不断收到过时的API警告。

The problem is that, no matter how I write down the Android OS check I keep getting the deprecated API warning.

我尝试了以下操作,对于所有可能的版本,我都会在 Build.SERIAL

I tried the following and for all possible versions I keep getting the warning on Build.SERIAL

private static String getSerial() throws SecurityException {
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
        return Build.getSerial();
    }
    return Build.SERIAL;
}

private static String getDeviceUDI() throws SecurityException {
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
        return Build.getSerial();
    } else {
        return Build.SERIAL;
    }
}

private static String getDeviceUDI() throws SecurityException {
    if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.O) {
        return Build.SERIAL;
    } else {
        return Build.getSerial();
    }
}


推荐答案

您需要这样的东西:

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

隐藏所有不正确的警告

这篇关于即使进行了正确的检查,我仍然不赞成使用API​​警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆