openharmony 鸿蒙 js-apis-bundleMonitor-sys

2025-06-12 浏览 (1)

@ohos.bundle.bundleMonitor (bundleMonitor) (System API)

The Bundle.bundleMonitor module provides APIs for listens for bundle installation, uninstall, and updates.

NOTE

The initial APIs of this module are supported since API version 9. Newly added APIs will be marked with a superscript to indicate their earliest API version.

The APIs provided by this module are system APIs.

Modules to Import

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

Required Permissions

PermissionAPLDescription
ohos.permission.LISTEN_BUNDLE_CHANGEsystem_basicPermission to listen for bundle installation, uninstall, and updates.

For details about the APL, see Basic Concepts in the Permission Mechanism.

BundleChangedInfo

System capability: SystemCapability.BundleManager.BundleFramework.Core

System API: This is a system API.

NameTypeRead-OnlyOptionalDescription
bundleNamestringYesNoName of the bundle whose status changes.
userIdnumberYesNoID of the user for whom the bundle status changes. You can obtain the ID by calling getOsAccountLocalId.

BundleChangedEvent

Enumerates the types of events to listen for.

System capability: SystemCapability.BundleManager.BundleFramework.Core

System API: This is a system API.

NameDescription
addBundle addition events.
updateBundle update events.
removeBundle removal events.

bundleMonitor.on

on(type: BundleChangedEvent, callback: Callback<BundleChangedInfo>): void

Subscribes to bundle installation, uninstall, and update events.

NOTE

This API must be used together with bundleMonitor.off. When the lifecycle of a component, page, or application ends, use bundleMonitor.off to unsubscribe from the bundle installation, uninstall, and update events.

Required permissions: ohos.permission.LISTEN_BUNDLE_CHANGE

System API: This is a system API.

System capability: SystemCapability.BundleManager.BundleFramework.Core

Parameters

NameTypeMandatoryDescription
typeBundleChangedEventYesType of the event to subscribe to.
callbackcallback<BundleChangedInfo>YesCallback used for the subscription.

Error codes

For details about the error codes, see Universal Error Codes.

IDError Message
201Verify permission denied.
202Permission denied, non-system app called system api.
401Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types.

Example

import bundleMonitor from '@ohos.bundle.bundleMonitor';
import { BusinessError } from '@ohos.base';

try {
    bundleMonitor.on('add', (bundleChangeInfo) => {
        console.info(`bundleName : ${bundleChangeInfo.bundleName} userId : ${bundleChangeInfo.userId}`);
	})
} catch (errData) {
    let message = (errData as BusinessError).message;
    let errCode = (errData as BusinessError).code;
    console.log(`errData is errCode:${errCode}  message:${message}`);
}

bundleMonitor.off

off(type: BundleChangedEvent, callback?: Callback<BundleChangedInfo>): void

Unsubscribes from bundle installation, uninstall, and update events.

Required permissions: ohos.permission.LISTEN_BUNDLE_CHANGE

System API: This is a system API.

System capability: SystemCapability.BundleManager.BundleFramework.Core

Parameters

NameTypeMandatoryDescription
typeBundleChangedEventYesType of the event to unsubscribe from.
callbackcallback<BundleChangedInfo>NoCallback used for the unsubscription. By default, no value is passed, and all callbacks of the current event are unsubscribed from.

Error codes

For details about the error codes, see Universal Error Codes.

IDError Message
201Verify permission denied.
202Permission denied, non-system app called system api.
401Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types.

Example

import bundleMonitor from '@ohos.bundle.bundleMonitor';
import { BusinessError } from '@ohos.base';

try {
    bundleMonitor.off('add');
} catch (errData) {
    let message = (errData as BusinessError).message;
    let errCode = (errData as BusinessError).code;
    console.log(`errData is errCode:${errCode}  message:${message}`);
}

你可能感兴趣的鸿蒙文章

harmony 鸿蒙Ability Kit

harmony 鸿蒙AbilityAccessControl

harmony 鸿蒙AbilityBase

harmony 鸿蒙AbilityBase_Element

harmony 鸿蒙AbilityRuntime

harmony 鸿蒙bundle

harmony 鸿蒙OH_NativeBundle_ApplicationInfo

harmony 鸿蒙OH_NativeBundle_ElementName

harmony 鸿蒙ability_access_control.h

harmony 鸿蒙ability_base_common.h

  • 所属分类: 后端技术
  • 本文标签: 软件 鸿蒙
  • 版权声明: 本文链接 https://seaxiang.com/blog/x7AWVK