一、简介
@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%')
}
}
