HarmonyOS:应用程序包管理模块(获取当前应用信息)

一、简介

@ohos.bundle.bundleManager (应用程序包管理模块)

本模块提供应用信息的查询能力,支持应用包信息BundleInfo 、应用程序信息ApplicationInfo 、UIAbility组件信息AbilityInfo 、ExtensionAbility组件信息ExtensionAbilityInfo等信息的查询。
说明 本模块首批接口从API version 9开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。

1.1 导入模块

c 复制代码
import { bundleManager } from '@kit.AbilityKit';

1.2 bundleManager.getBundleInfoForSelf接口

getBundleInfoForSelf(bundleFlags: number): Promise<BundleInfo>

根据给定的bundleFlags获取当前应用的BundleInfo。使用Promise异步回调。

Meta Service API : Starting from API version 11, this interface supports usage in Meta Services.

系统能力: SystemCapability.BundleManager.BundleFramework.Core

参数:

参数名 类型 必填 说明
bundleFlags number 是 指定返回的BundleInfo所包含的信息。

返回值:

类型 说明
Promise<BundleInfo> Promise对象,返回当前应用的BundleInfo。

二、示例

效果图

示例代码 BundleManagerUtil.ets

c 复制代码
import { bundleManager } from "@kit.AbilityKit";
import { BundleInfoData } from "../model/BundleInfoData";
import { JSON } from "@kit.ArkTS";

const TAG = "BundleManagerUtil,"

export class BundleManagerUtil {
  public static getBundleInfo(): Promise<string> {
    try {
      return bundleManager.getBundleInfoForSelf(bundleManager.BundleFlag.GET_BUNDLE_INFO_WITH_APPLICATION)
        .then((bundleInfo: bundleManager.BundleInfo) => {
          const appInfo: bundleManager.ApplicationInfo = bundleInfo.appInfo;
          console.log(TAG + "appInfo:" + JSON.stringify(appInfo))
          const bundleInfoData: BundleInfoData =
            new BundleInfoData(bundleInfo.versionName, bundleInfo.versionCode, bundleInfo.name);
          AppStorage.setOrCreate('bundle_info', bundleInfoData);
          return Promise.resolve(JSON.stringify(appInfo));
        })
        .catch((error: BusinessError) => {
          console.error(TAG, `getBundleInfoForSelf failed: code ${error.code}, message ${error.message}`);
          return Promise.reject("获取app信息 发生异常");
        });
    } catch (e) {
      const error = e as BusinessError;
      console.error(TAG, `getBundleInfoForSelf failed: code ${error.code}, message ${error.message}`);
      return Promise.reject("获取app信息 发生异常");
    }
  }
}

BundleInfoData.ets

c 复制代码
export class BundleInfoData {
  public versionName: string;
  public versionCode: number;
  public bundleName: string;

  public constructor(versionName: string, versionCode: number,
    bundleName: string) {
    this.versionName = versionName ?? '1.0.0';
    this.versionCode = versionCode ?? 1000000;
    this.bundleName = bundleName ?? 'com.test.app';
  }
}

TestGetAppInfo.ets

c 复制代码
import { BundleManagerUtil } from '../../common/utils/BundleManagerUtil';

@Entry
@Component
struct TestGetAppInfo {
  @State appinfo: string = '';

  build() {
    Column({ space: 20 }) {
      if (this.appinfo) {
        Scroll() {
          Text(this.appinfo)
            .id('TestGetAppInfoHelloWorld')
            .fontSize($r('app.float.text_size_20fp'))
            .margin({ left: 16, right: 16 })
        }
        .height(600)
        .margin({ top: 40 })
      }
      Button("查看app信息").onClick(() => [
        BundleManagerUtil.getBundleInfo().then((appInfo) => {
          this.appinfo = appInfo
          console.log("查看app信息返回的数据:", appInfo)
        })
      ])
        .margin({ top: 40 })
    }
    .height('100%')
    .width('100%')
  }
}
相关推荐
二流小码农20 小时前
鸿蒙开发:router组件升级,彻底告别手动配置
android·ios·harmonyos
威哥爱编程1 天前
模拟面试智能体开发实战 09:把面试官装进手机,鸿蒙端聊天界面用 SSE 流式输出让回复逐字滚出来
python·agent·harmonyos
威哥爱编程1 天前
模拟面试智能体开发实战 10:配置写死在代码里还怎么换服务端,把设置和简历做成界面可配、可存
python·agent·harmonyos
Sirius_Ji2 天前
# 一份鸿蒙原生支持硬解的 ffmpeg 开发基座
harmonyos
贾伟康2 天前
HarmonyOS API 26 Navigation路由栈上限:stackSizeLimit、过载保护与容量观测
harmonyos·arkts·navigation·navpathstack·api26
江华森2 天前
把鸿蒙云手机调试的 5 个坑写成 Skill:12 项检查清单与报错决策树
harmonyos
木木子222 天前
用华为云码道构建校园导航应用「新生通」:从需求分析到工程落地
华为·华为云·harmonyos
贾伟康2 天前
HarmonyOS API 26 Navigation 自适应模式:mode、分栏切换与窗口断点治理
harmonyos·arkts·arkui·navigation·自适应布局
贾伟康2 天前
【转盘决定吧|07】HarmonyOS ArkTS 6.0.2.130 趣味题库:场景分类、随机选择与详情页解耦
harmonyos·arkts·路由·数据建模·arkui
贾伟康2 天前
【电路实验模拟|03】HarmonyOS ArkTS 交通灯实战:用定时状态切换红黄绿灯
harmonyos·arkts·canvas·arkui·交通灯