HarmonyOS 鸿蒙Next 获取应用包名

HarmonyOS 鸿蒙Next 获取应用包名

public static getBundleName() { return globalThis.mainAbilityContext.applicationInfo.name }

globalThis.mainAbilityContext 可以替换为getContext()

1 回复

更多关于HarmonyOS 鸿蒙Next 获取应用包名的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


在HarmonyOS 鸿蒙Next中,获取应用包名可以通过BundleManager类实现。BundleManager是鸿蒙系统提供的用于管理应用包信息的工具类。可以通过BundleManagergetBundleInfo方法获取应用的包信息,其中包含包名等详细信息。

具体步骤如下:

  1. 导入相关模块:

    import bundleManager from '[@ohos](/user/ohos).bundle.bundleManager';
    
  2. 使用getBundleInfo方法获取包信息:

    bundleManager.getBundleInfo(bundleName, bundleManager.BundleFlag.GET_BUNDLE_INFO_DEFAULT)
      .then((data) => {
        console.log('Package name: ' + data.name);
      })
      .catch((error) => {
        console.error('Failed to get bundle info. Error: ' + error);
      });
    

其中,bundleName是应用的标识符,data.name即为应用的包名。

回到顶部