鸿蒙读取三方应用版本号

申请权限:"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%')
  }
}
相关推荐
echohelloworld1113 小时前
HarmonyOS应用开发实战:猫猫大作战-KV Store 跨端数据同步
harmonyos·鸿蒙
molihuan16 小时前
最新 spine 4.3 flutter 适配鸿蒙
flutter·动画·harmonyos·鸿蒙·spine
FF2501_9402285818 小时前
HarmonyOS应用开发实战:猫猫大作战-AVPlayer 音视频播放
harmonyos·鸿蒙
shaodong112318 小时前
HarmonyOS 多设备开发实践:我把 SysCap、断点和折叠屏适配放到了同一套架构里
华为·架构·harmonyos·鸿蒙
熊猫钓鱼>_>21 小时前
ArkTS 基础入门:从零搭建第一个交互页面
华为·架构·交互·ts·harmonyos·arkts·鸿蒙
三声三视21 小时前
DevEco Code 让 AI 写 ArkTS,enum 反向查表真机翻车——我换成 typeof + as const 后清净了
人工智能·harmonyos·arkts·鸿蒙
爱写代码的阿森1 天前
鸿蒙三方库 | harmony-utils之EncryptUtil AES对称加密详解
华为·harmonyos·鸿蒙·huawei
爱写代码的阿森1 天前
鸿蒙三方库 | harmony-utils之FileUtil文件管理与目录详解
华为·harmonyos·鸿蒙·huawei
爱写代码的森1 天前
鸿蒙三方库 | harmony-utils之DateUtil日期计算详解
华为·harmonyos·鸿蒙·huawei
FF2501_940228582 天前
HarmonyOS应用《玄象》开发实战:FortuneTimelinePage 运势时间线:Timeline 自定义组件 + List 虚拟滚动
harmonyos·鸿蒙