草庐IT

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

小枫_S 2025-07-26 原文

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

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

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

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的兼容性调用,解决了可能出现的运行时闪退问题。

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

有关【鸿蒙应用开发系列】- 获取系统设备信息以及版本API兼容调用方式的更多相关文章

  1. python - 如何使用 Ruby 或 Python 创建一系列高音调和低音调的蜂鸣声? - 2

    关闭。这个问题是opinion-based.它目前不接受答案。想要改进这个问题?更新问题,以便editingthispost可以用事实和引用来回答它.关闭4年前。Improvethisquestion我想在固定时间创建一系列低音和高音调的哔哔声。例如:在150毫秒时发出高音调的蜂鸣声在151毫秒时发出低音调的蜂鸣声200毫秒时发出低音调的蜂鸣声250毫秒的高音调蜂鸣声有没有办法在Ruby或Python中做到这一点?我真的不在乎输出编码是什么(.wav、.mp3、.ogg等等),但我确实想创建一个输出文件。

  2. ruby - 将差异补丁应用于字符串/文件 - 2

    对于具有离线功能的智能手机应用程序,我正在为Xml文件创建单向文本同步。我希望我的服务器将增量/差异(例如GNU差异补丁)发送到目标设备。这是计划:Time=0Server:hasversion_1ofXmlfile(~800kiB)Client:hasversion_1ofXmlfile(~800kiB)Time=1Server:hasversion_1andversion_2ofXmlfile(each~800kiB)computesdeltaoftheseversions(=patch)(~10kiB)sendspatchtoClient(~10kiBtransferred)Cl

  3. ruby-on-rails - Rails 常用字符串(用于通知和错误信息等) - 2

    大约一年前,我决定确保每个包含非唯一文本的Flash通知都将从模块中的方法中获取文本。我这样做的最初原因是为了避免一遍又一遍地输入相同的字符串。如果我想更改措辞,我可以在一个地方轻松完成,而且一遍又一遍地重复同一件事而出现拼写错误的可能性也会降低。我最终得到的是这样的:moduleMessagesdefformat_error_messages(errors)errors.map{|attribute,message|"Error:#{attribute.to_s.titleize}#{message}."}enddeferror_message_could_not_find(obje

  4. ruby - 什么是填充的 Base64 编码字符串以及如何在 ruby​​ 中生成它们? - 2

    我正在使用的第三方API的文档状态:"[O]urAPIonlyacceptspaddedBase64encodedstrings."什么是“填充的Base64编码字符串”以及如何在Ruby中生成它们。下面的代码是我第一次尝试创建转换为Base64的JSON格式数据。xa=Base64.encode64(a.to_json) 最佳答案 他们说的padding其实就是Base64本身的一部分。它是末尾的“=”和“==”。Base64将3个字节的数据包编码为4个编码字符。所以如果你的输入数据有长度n和n%3=1=>"=="末尾用于填充n%

  5. ruby - 解析 RDFa、微数据等的最佳方式是什么,使用统一的模式/词汇(例如 schema.org)存储和显示信息 - 2

    我主要使用Ruby来执行此操作,但到目前为止我的攻击计划如下:使用gemsrdf、rdf-rdfa和rdf-microdata或mida来解析给定任何URI的数据。我认为最好映射到像schema.org这样的统一模式,例如使用这个yaml文件,它试图描述数据词汇表和opengraph到schema.org之间的转换:#SchemaXtoschema.orgconversion#data-vocabularyDV:name:namestreet-address:streetAddressregion:addressRegionlocality:addressLocalityphoto:i

  6. ruby - 使用 C 扩展开发 ruby​​gem 时,如何使用 Rspec 在本地进行测试? - 2

    我正在编写一个包含C扩展的gem。通常当我写一个gem时,我会遵循TDD的过程,我会写一个失败的规范,然后处理代码直到它通过,等等......在“ext/mygem/mygem.c”中我的C扩展和在gemspec的“扩展”中配置的有效extconf.rb,如何运行我的规范并仍然加载我的C扩展?当我更改C代码时,我需要采取哪些步骤来重新编译代码?这可能是个愚蠢的问题,但是从我的gem的开发源代码树中输入“bundleinstall”不会构建任何native扩展。当我手动运行rubyext/mygem/extconf.rb时,我确实得到了一个Makefile(在整个项目的根目录中),然后当

  7. ruby-on-rails - Rails 应用程序之间的通信 - 2

    我构建了两个需要相互通信和发送文件的Rails应用程序。例如,一个Rails应用程序会发送请求以查看其他应用程序数据库中的表。然后另一个应用程序将呈现该表的json并将其发回。我还希望一个应用程序将存储在其公共(public)目录中的文本文件发送到另一个应用程序的公共(public)目录。我从来没有做过这样的事情,所以我什至不知道从哪里开始。任何帮助,将不胜感激。谢谢! 最佳答案 无论Rails是什么,几乎所有Web应用程序都有您的要求,大多数现代Web应用程序都需要相互通信。但是有一个小小的理解需要你坚持下去,网站不应直接访问彼此

  8. ruby - 无法运行 Rails 2.x 应用程序 - 2

    我尝试运行2.x应用程序。我使用rvm并为此应用程序设置其他版本的ruby​​:$rvmuseree-1.8.7-head我尝试运行服务器,然后出现很多错误:$script/serverNOTE:Gem.source_indexisdeprecated,useSpecification.Itwillberemovedonorafter2011-11-01.Gem.source_indexcalledfrom/Users/serg/rails_projects_terminal/work_proj/spohelp/config/../vendor/rails/railties/lib/r

  9. ruby-on-rails - Rails 应用程序中的 Rails : How are you using application_controller. rb 是新手吗? - 2

    刚入门rails,开始慢慢理解。有人可以解释或给我一些关于在application_controller中编码的好处或时间和原因的想法吗?有哪些用例。您如何为Rails应用程序使用应用程序Controller?我不想在那里放太多代码,因为据我了解,每个请求都会调用此Controller。这是真的? 最佳答案 ApplicationController实际上是您应用程序中的每个其他Controller都将从中继承的类(尽管这不是强制性的)。我同意不要用太多代码弄乱它并保持干净整洁的态度,尽管在某些情况下ApplicationContr

  10. ruby-on-rails - 使用一系列等级计算字母等级 - 2

    这里是Ruby新手。完成一些练习后碰壁了。练习:计算一系列成绩的字母等级创建一个方法get_grade来接受测试分数数组。数组中的每个分数应介于0和100之间,其中100是最大分数。计算平均分并将字母等级作为字符串返回,即“A”、“B”、“C”、“D”、“E”或“F”。我一直返回错误:avg.rb:1:syntaxerror,unexpectedtLBRACK,expecting')'defget_grade([100,90,80])^avg.rb:1:syntaxerror,unexpected')',expecting$end这是我目前所拥有的。我想坚持使用下面的方法或.join,

随机推荐