鸿蒙读取三方应用版本号

申请权限:"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%')
  }
}
相关推荐
左手厨刀右手茼蒿2 天前
Flutter 组件 sheety_localization 的适配 鸿蒙Harmony 实战 - 驾驭在线协作式多语言管理、实现鸿蒙端动态词条下发与全球化敏捷发布方案
flutter·harmonyos·鸿蒙·openharmony·sheety_localization
星释2 天前
鸿蒙Flutter实战:30.在Pub上发布鸿蒙化插件
flutter·harmonyos·鸿蒙
梁山好汉(Ls_man)2 天前
鸿蒙应用如何新建页面
华为·harmonyos·鸿蒙·arkui
睿麒3 天前
json2est 的用法
鸿蒙
SY.ZHOU3 天前
移动端架构体系(五):终篇总结
flutter·ios·系统架构·安卓·鸿蒙
@不误正业3 天前
AI-Agent记忆系统深度实战-3大范式源码对比与鸿蒙端实现
人工智能·agent·鸿蒙
梁山好汉(Ls_man)4 天前
鸿蒙_自定义组件包含多个引用自定义构建函数@BuilderParam时的用法
华为·harmonyos·鸿蒙·arkui
UnicornDev4 天前
【HarmonyOS 6】鸿蒙原生应用智能体接入
华为·harmonyos·arkts·鸿蒙·鸿蒙系统
梦想不只是梦与想4 天前
鸿蒙中 PhotoViewPicker:选择图片或视频
harmonyos·鸿蒙·photoviewpicker
星释4 天前
鸿蒙Flutter实战:29.优先使用联合插件开发鸿蒙化插件
flutter·华为·harmonyos·鸿蒙