HarmonyOS 如何在har包中获得当前应用的包名、活动的AbilityName以及活动的ModuleName

NFC读卡接口需要传递bundleManager.ElementName,其中包括bundleName、abilityName和moduleName若是读卡逻辑写在har包内,如何获取bundleName、abilityName和moduleName

HarmonyOS
2024-12-20 16:14:35
1541浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
put_get

har包中可以通过外部传参取得bundleName、abilityName和moduleName 请见下方示例:

hap包中的页面(OutPage)调用har包导出的组件页面(MainPage)

import { MainPage } from 'harApp/Index';
import { common } from '@kit.AbilityKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
const TAG: string = 'testTag'
@Entry
@Component
struct OutPage {
  @State message: string = 'OutPage';
  private bundleName: string = ''
  private abilityName: string = ''
  private moduleName: string = ''
  aboutToAppear(): void {
    let context = getContext() as common.UIAbilityContext
    hilog.info(0x0000, TAG, 'OutPage-bundleName为:%{public}s', context.abilityInfo.bundleName)
    hilog.info(0x0000, TAG, 'OutPage-abilityName为:%{public}s', context.abilityInfo.name)
    hilog.info(0x0000, TAG, 'OutPage-moduleName为:%{public}s', context.abilityInfo.moduleName)
    this.bundleName = context.abilityInfo.bundleName
    this.abilityName = context.abilityInfo.name
    this.moduleName = context.abilityInfo.moduleName
  }
  build() {
    Column() {
      MainPage({ bundleName: this.bundleName, abilityName: this.abilityName, moduleName: this.moduleName })
    }
  }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
分享
微博
QQ
微信
回复
2024-12-20 18:41:20


相关问题
如何获取当前HAP
3291浏览 • 1回复 待解决
怎么获得当前设备IP地址?
1002浏览 • 1回复 待解决
鸿蒙JS UI如何获得当前组件值?
7423浏览 • 1回复 待解决
HarmonyOS 应用命名规则
2213浏览 • 1回复 待解决
HarmonyOS 系统里如何查看应用
2411浏览 • 1回复 待解决
harworker如何在entry内使用
2899浏览 • 1回复 待解决
HarmonyOS 有没有api可以改应用
1046浏览 • 1回复 待解决