HarmonyOS鸿蒙Next中如何获取当前APP包名

HarmonyOS鸿蒙Next中如何获取当前APP包名 如何获取当前APP包名

4 回复
app包名可以通过应用context获取 
```javascript
console.log(this.context.applicationInfo.name)

参考链接:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-inner-application-context-V5

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


let abilityContext: common.UIAbilityContext = getContext(context) as common.UIAbilityContext
abilityContext.abilityInfo.bundleName

在HarmonyOS(鸿蒙)Next中,获取当前APP包名可以通过BundleManager类来实现。具体步骤如下:

  1. 首先,获取当前应用的上下文(Context),通常可以通过getContext()getAbilityContext()来获取。

  2. 使用BundleManager类中的getBundleInfoForSelf()方法获取当前应用的BundleInfo对象。

  3. BundleInfo对象中提取bundleName属性,即为当前APP的包名。

示例代码如下:

import bundleManager from '@ohos.bundle.bundleManager';

let context = getContext(); // 获取当前应用的上下文
let bundleInfo = bundleManager.getBundleInfoForSelf(); // 获取当前应用的BundleInfo
let packageName = bundleInfo.bundleName; // 获取当前APP的包名

在HarmonyOS鸿蒙Next中,可以通过BundleManager类获取当前应用的包名。具体步骤如下:

  1. 导入相关类:

    import ohos.bundle.BundleManager;
    import ohos.bundle.IBundleManager;
    
  2. 获取BundleManager实例:

    IBundleManager bundleManager = getContext().getBundleManager();
    
  3. 获取当前应用的包名:

    String packageName = bundleManager.getBundleInfo().getPackageName();
    
回到顶部