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

添加到 module.json5 中

index.ets

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%')
  }
}

Logo

讨论HarmonyOS开发技术,专注于API与组件、DevEco Studio、测试、元服务和应用上架分发等。

更多推荐