当前位置:   article > 正文

【鸿蒙应用开发系列】- 获取系统设备信息以及版本API兼容调用方式_鸿蒙 获取设备信息

鸿蒙 获取设备信息

在应用开发中,有时候我们需要获取系统的设备信息,用于数据上报和行为分析。

那在鸿蒙系统中,我们应该怎么去获取设备的系统信息呢,比如说获取手机的系统版本号、手机的制造商、手机型号等数据。

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

getBootloaderVersion​()

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

getBuildRootHash​()

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

getDeviceType​()

Obtains the device type represented by a string.

static String

getDisplayVersion​()

Obtains the display version represented by a string.

static String

getHardwareModel​()

Obtains the hardware version represented by a string.

static String

getHardwareProfile​()

Obtains the hardware profile represented by a string.

static String

getIncrementalVersion​()

Obtains the incremental version represented by a string.

static String

getLocale​()

Obtains the product locale represented by a string.

static String

getLocaleLanguage​()

Obtains the product locale's language represented by a string.

static String

getLocaleRegion​()

Obtains the product locale's region represented by a string.

static String

getManufacture​()

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

getOsReleaseType​()

Obtains the os release type represented by a string.

static String

getProductSeries​()

Obtains the product series represented by a string.

static String

getSecurityPatchTag​()

Obtains the security patch tag represented by a string.

static String

getSoftwareModel​()

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

getApiVersion​()

Obtains the API version number.

static int

getBuildVersion​()

Obtains the build (B) version number, which increases with each new development build.

static int

getFeatureVersion​()

Obtains the feature (F) version number, which increases with any planned new features.

static int

getFirstApiVersion​()

Obtains the First API version number.

static int

getMajorVersion​()

Obtains the major (M) version number, which increases with any updates to the overall architecture.

static String

getOSFullName​()

Obtains the OS full name.

static String

getReleaseType​()

Obtains the OS release type.

static int

getSdkApiVersion​()

Obtains the SDK API version number.

static int

getSeniorVersion​()

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版本定义,具体如下

  1. public class Build {
  2. public static class VERSION {
  3. public static final int CODES_1 = 1;
  4. public static final int CODES_2 = 2;
  5. public static final int CODES_3 = 3;
  6. public static final int CODES_4 = 4;
  7. public static final int CODES_5 = 5;
  8. public static final int CODES_6 = 6;
  9. public static final int CODES_7 = 7;
  10. }
  11. }


对外回传一个空值,或者传递一个UNKNOWN字符串。在调用getManufacturer 的地方,增加系统SDK版本的判断,当我们的系统SDK版本号满足调用该方法的条件下,才去调用getManufacturer方法,否则,对外回传一个空值,或者传递一个UNKNOWN字符串。

  1. public static String getManufacturer() {
  2. if (SystemVersion.getApiVersion()>= Build.VERSION.CODES_7) {
  3. return checkValidData(DeviceInfo.getManufacture());
  4. }
  5. return "UNKNOWN";
  6. }
  7. private static String checkValidData(final String data) {
  8. String tempData = data;
  9. if (tempData == null || tempData.length() == 0) {
  10. tempData = "";
  11. }
  12. return tempData;
  13. }

这里我们调用SystemVersion.getApiVersion()得到一个系统的API版本号,就是上面说的支持的SDK版本号,通过该版本号可以知道当前使用的手机系统最高

支持的SDK版本具体是多少,这样就完成了我们调用鸿蒙开发SDK包中的API的兼容性调用,解决了可能出现的运行时闪退问题。

本文到此就完结了,谢谢大家的阅读!

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/321933
推荐阅读
相关标签
  

闽ICP备14008679号