在应用开发中,有时候我们需要获取系统的设备信息,用于数据上报和行为分析。
那在鸿蒙系统中,我们应该怎么去获取设备的系统信息呢,比如说获取手机的系统版本号、手机的制造商、手机型号等数据。
1、获取方式
这里分为两种情况,一种是设备信息的获取,一种是系统信息的获取。
1.1、获取设备信息
获取设备信息,鸿蒙的SDK包为我们提供了DeviceInfo类,通过该类的一些静态方法,可以获取设备信息,DeviceInfo类的包路径为:
ohos.system.DeviceInfo.
具体的方法如下:
Modifier and Type | Method | Description |
---|---|---|
static String | getAbiList() | Obtains the abilist represented by a string. |
static String | Obtains the bootloader version represented by a string. | |
static String | getBrand() | Obtains the brand represented by a string. |
static String | getBuildHost() | Obtains the build host represented by a string. |
static String | Obtains the build root hash represented by a string. | |
static String | getBuildTime() | Obtains the build time represented by a string. |
static String | getBuildType() | Obtains the build type represented by a string. |
static String | getBuildUser() | Obtains the build user represented by a string. |
static String | Obtains the device type represented by a string. | |
static String | Obtains the display version represented by a string. | |
static String | Obtains the hardware version represented by a string. | |
static String | Obtains the hardware profile represented by a string. | |
static String | Obtains the incremental version represented by a string. | |
static String | getLocale() | Obtains the product locale represented by a string. |
static String | Obtains the product locale's language represented by a string. | |
static String | Obtains the product locale's region represented by a string. | |
static String | Obtains the manufacture represented by a string. | |
static String | getModel() | Obtains the product model represented by a string. |
static String | getName() | Obtains the product name represented by a string. |
static String | Obtains the os release type represented by a string. | |
static String | Obtains the product series represented by a string. | |
static String | Obtains the security patch tag represented by a string. | |
static String | Obtains the software model represented by a string. | |
static String | getVersionId() | Obtains the version id represented by a string. |
1.2、获取系统信息
获取系统信息,鸿蒙的SDK包为我们提供了SystemVersion类,通过该类的一些静态方法,可以获取设备信息,SystemVersion类的包路径为:
ohos.system.version.SystemVersion
具体的方法如下:
Modifier and Type | Method | Description |
---|---|---|
static int | Obtains the API version number. | |
static int | Obtains the build (B) version number, which increases with each new development build. | |
static int | Obtains the feature (F) version number, which increases with any planned new features. | |
static int | Obtains the First API version number. | |
static int | Obtains the major (M) version number, which increases with any updates to the overall architecture. | |
static String | Obtains the OS full name. | |
static String | Obtains the OS release type. | |
static int | Obtains the SDK API version number. | |
static int | Obtains the senior (S) version number, which increases with any updates to the partial architecture or major features. | |
static String | getVersion() | Obtains the version number represented by a string in the following format: Major version number.Senior version number.Feature version number.Build version number. |
2、API兼容调用方式
上述鸿蒙提供的DeviceInfo、SystemVersion类,其中一些方法,是在SDK7 中才提供的,换句话说,就是在SDK6中是不存在这些方法的,因此,当我们
的应用运行在SDK6的鸿蒙系统中,比如鸿蒙2.0系统上,当我们使用了这些方法(API),会出现运行时错误,导致引用闪退。
比如我们在鸿蒙系统2.0的手机上,应用调用getManufacturer方法,会出现如下错误:
java.lang.NoSuchMethodError: No static method getManufacture()Ljava/lang/String; in class Lohos/system/DeviceInfo; or its super classes (declaration of 'ohos.system.DeviceInfo' appears in /system/framework/zframework.z.jar!classes2.dex)
at com.xxx.xxx.AppUtils.getManufacturer(AppUtils.java:10)
通过日志,可以很明显的看出报出的错误是未找到方法的错误。
那我们应该怎么解决上述问题呢,在不支持的系统中,兼容处理这种错误,下面为大家提供一种解决方案。
我们定义一个Build,提供一些系统SDK版本定义,具体如下
public class Build {
public static class VERSION {
public static final int CODES_1 = 1;
public static final int CODES_2 = 2;
public static final int CODES_3 = 3;
public static final int CODES_4 = 4;
public static final int CODES_5 = 5;
public static final int CODES_6 = 6;
public static final int CODES_7 = 7;
}
}
对外回传一个空值,或者传递一个UNKNOWN字符串。在调用getManufacturer 的地方,增加系统SDK版本的判断,当我们的系统SDK版本号满足调用该方法的条件下,才去调用getManufacturer方法,否则,对外回传一个空值,或者传递一个UNKNOWN字符串。
public static String getManufacturer() {
if (SystemVersion.getApiVersion()>= Build.VERSION.CODES_7) {
return checkValidData(DeviceInfo.getManufacture());
}
return "UNKNOWN";
}
private static String checkValidData(final String data) {
String tempData = data;
if (tempData == null || tempData.length() == 0) {
tempData = "";
}
return tempData;
}
这里我们调用SystemVersion.getApiVersion()得到一个系统的API版本号,就是上面说的支持的SDK版本号,通过该版本号可以知道当前使用的手机系统最高
支持的SDK版本具体是多少,这样就完成了我们调用鸿蒙开发SDK包中的API的兼容性调用,解决了可能出现的运行时闪退问题。
本文到此就完结了,谢谢大家的阅读!