HarmonyOS 如何获取app的名称?

该如何获取app的名称,在非entry模块或任意地方

阅读 444
1 个回答

参考示例:

import { bundleManager } from '@kit.AbilityKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { BusinessError } from '@kit.BasicServicesKit';
const TAG = 'HAR'

...
...

let bundleFlags = bundleManager.BundleFlag.GET_BUNDLE_INFO_WITH_APPLICATION;
try {
  bundleManager.getBundleInfoForSelf(bundleFlags).then((data) => {
    hilog.info(0x0000, 'testTag', 'getBundleInfoForSelf successfully. Data: %{public}s', JSON.stringify(data));
    let bundle_info = data
    // app 名称
    let label  = bundle_info.appInfo.label
    console.log(TAG,'label is: ',label)
    getContext().resourceManager.getStringByName(label.split(':')[1],(error:BusinessError , value:string)=>{
      console.log(TAG,'label text is : ', value)
    })
    // console.log()
  }).catch((err: BusinessError) => {
    hilog.error(0x0000, 'testTag', 'getBundleInfoForSelf failed. Cause: %{public}s', err.message);
  });
} catch (err) {
  let message = (err as BusinessError).message;
  hilog.error(0x0000, 'testTag', 'getBundleInfoForSelf failed: %{public}s', message);
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进