鸿蒙读取三方应用版本号

申请权限:"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%')
  }
}
相关推荐
UnicornDev3 小时前
【HarmonyOS 6】活动标签管理页面实现
华为·harmonyos·arkts·鸿蒙·鸿蒙系统
ShuiShenHuoLe6 小时前
管理数据的状态
harmonyos·鸿蒙
坚果派·白晓明2 天前
三方库ada
harmonyos·鸿蒙·openharmony
云和数据.ChenGuang2 天前
鸿蒙餐饮系统:全场景智慧餐饮新范式
人工智能·机器学习·华为·数据挖掘·harmonyos·鸿蒙·鸿蒙系统
云和数据.ChenGuang3 天前
鸿蒙智联,极智共生:HarmonyOS与MiniMax智能体的融合新纪元
华为·harmonyos·鸿蒙
UnicornDev3 天前
【HarmonyOS 6】今日统计卡片实战:运动记录数据概览
华为·harmonyos·arkts·鸿蒙·鸿蒙系统
ShuiShenHuoLe3 天前
组件的状态ComponentV2
harmonyos·鸿蒙
仓颉编程语言4 天前
CangjieSkills 正式开源:为仓颉 AI 编程打造的“技能增强“方案,实测降低 60% 费用
华为·ai编程·鸿蒙·仓颉编程语言
左手厨刀右手茼蒿5 天前
Flutter 三方库 all_lint_rules_community 的鸿蒙化适配指南 - 在鸿蒙系统上构建极致、严谨、基于全量社区 Lint 规则的工业级静态代码质量与安全审计引擎
flutter·harmonyos·鸿蒙·openharmony·all_lint_rules_community