打赏

鸿蒙获取appIdentifier值得方法

import { bundleManager } from '@kit.AbilityKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { BusinessError } from '@kit.BasicServicesKit';

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

  // 【写在struct里面,build外面】点击触发获取
  async getAppId() {
    try {
      const bundleInfo = await bundleManager.getBundleInfoForSelf(
        bundleManager.BundleFlag.GET_BUNDLE_INFO_WITH_SIGNATURE_INFO
      );
      const appId = bundleInfo.signatureInfo.appIdentifier;
      hilog.info(0x0001, "testTag", "====appIdentifier====" + appId);
      console.log("appIdentifier:", appId);
      this.message = "appIdentifier:" + appId;
    } catch (err: BusinessError) {
      let errorMsg = JSON.stringify(err);
      hilog.error(0x0001, "testTag", "error:" + errorMsg);
      this.message = "获取失败:" + errorMsg;
    }
  }

  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.getAppId();
        })
    }
    .height('100%')
    .width('100%')
  }
}

  

 
posted @ 2026-09-17 10:22  吃瓜大湿熊  阅读(5)  评论(0)    收藏  举报