鸿蒙读取三方应用版本号

申请权限:"ohos.permission.GET_BUNDLE_INFO_PRIVILEGED"

添加到 module.json5 中

index.ets

javascript 复制代码
import { bundleManager } from '@kit.AbilityKit';
import { BusinessError } from '@kit.BasicServicesKit';

@Entry
@Component
struct Index {
  @State message: string = 'Hello World';

  onPageShow(): void {
    // 目标三方应用的包名(如:com.example.app)
    let targetBundleName: string = "com.sanway.ecoengine";

    bundleManager.getBundleInfo(targetBundleName,
      bundleManager.BundleFlag.GET_BUNDLE_INFO_WITH_APPLICATION,
      100,
      (err: BusinessError, data) => {
        if (err) {
          this.message = `Failed to query target app. Code: ${err.code}`
          console.error(`Failed to query target app. Code: ${err.code}`);
          return;
        }
        this.message = `Target app version: ${data.versionName}`
        console.info(`Target app version: ${data.versionName}`);
      }
    );
  }

  build() {
    RelativeContainer() {
      Text(this.message)
        .id('HelloWorld')
        .fontSize($r('app.float.page_text_font_size'))
        .fontWeight(FontWeight.Bold)
        .alignRules({
          center: { anchor: '__container__', align: VerticalAlign.Center },
          middle: { anchor: '__container__', align: HorizontalAlign.Center }
        })
        .onClick(() => {
          this.message = 'Welcome';
        })
    }
    .height('100%')
    .width('100%')
  }
}
相关推荐
世人万千丶3 天前
桌面便签小应用 - HarmonyOS ArkUI 开发实战-TextArea与Flex布局-PC版本
华为·harmonyos·鸿蒙·鸿蒙系统
YM52e3 天前
买菜计算器小应用 - HarmonyOS ArkUI 开发实战-PC版本
学习·华为·harmonyos·鸿蒙·鸿蒙系统
小雨下雨的雨3 天前
HarmonyOS ArkUI训练营入门-组件掌握系列-Animation 动画效果实现-PC版本
学习·华为·harmonyos·鸿蒙
三声三视3 天前
Electron 在鸿蒙 PC 上跑 webview,我是怎么把首屏从 4.2s 干到 1.1s 的
华为·electron·harmonyos·鸿蒙
世人万千丶3 天前
成语接龙小应用 - HarmonyOS ArkUI 开发实战-TextInput与List列表-PC版本
华为·list·harmonyos·鸿蒙·鸿蒙系统
Davina_yu3 天前
自定义弹窗:使用CustomDialogController实现复杂交互(27)
harmonyos·鸿蒙·鸿蒙系统
世人万千丶3 天前
家庭记账本小应用 - HarmonyOS ArkUI 开发实战-Tabs与List组件-PC版本
华为·list·harmonyos·鸿蒙
Davina_yu3 天前
画布Canvas:2D绘图上下文(Context2D)绘制复杂图表(33)
harmonyos·鸿蒙·鸿蒙系统
小雨下雨的雨3 天前
HarmonyOS ArkUI训练营入门-组件掌握系列-Grid 网格布局深度解析-PC版本
学习·华为·harmonyos·鸿蒙·鸿蒙系统
Davina_yu3 天前
定时器与任务调度:setTimeout与setInterval的正确使用(19)
harmonyos·鸿蒙·鸿蒙系统