openharmony 鸿蒙 js-apis-Bundle-sys

2025-06-12 浏览 (1)

@ohos.bundle (Bundle) (System API)

The bundle module provides APIs for obtaining information about an application, including bundle information, application information, and ability information. It also provides APIs to obtain and set the application disabling state.

NOTE

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

The APIs of this module are deprecated since API version 9. You are advised to use @ohos.bundle.bundleManager instead.

This topic describes only system APIs provided by the module. For details about its public APIs, see @ohos.bundle.

Modules to Import

import bundle from '@ohos.bundle';

Required Permissions

PermissionAPLDescription
ohos.permission.CHANGE_ABILITY_ENABLED_STATEsystem_basicPermission to enable or disable an application or ability.
ohos.permission.GET_BUNDLE_INFOnormalPermission to query information about a specified bundle.
ohos.permission.GET_BUNDLE_INFO_PRIVILEGEDsystem_basicPermission to query information about all bundles.
ohos.permission.INSTALL_BUNDLEsystem_corePermission to install or uninstall bundles.
ohos.permission.REMOVE_CACHE_FILESsystem_basicPermission to clear cache files of a bundle.

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

bundle.getBundleInstallerdeprecated

This API is deprecated since API version 9. You are advised to use installer.getBundleInstaller instead.

getBundleInstaller(): Promise<BundleInstaller>

Obtains the installation package. This API uses a promise to return the result.

Required permissions

ohos.permission.INSTALL_BUNDLE

System capability

SystemCapability.BundleManager.BundleFramework

System API

This is a system API.

Return value

TypeDescription
Promise<BundleInstaller>Promise used to return the installation package.

Example

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

bundle.getBundleInstaller().then((data) => {
  console.info('getBundleInstaller successfully.');
}).catch((error: BusinessError) => {
  console.error('getBundleInstaller failed.');
});

bundle.getBundleInstallerdeprecated

This API is deprecated since API version 9. You are advised to use installer.getBundleInstaller instead.

getBundleInstaller(callback: AsyncCallback<BundleInstaller>): void

Obtains the installation package. This API uses an asynchronous callback to return the result.

Required permissions

ohos.permission.INSTALL_BUNDLE

System capability

SystemCapability.BundleManager.BundleFramework

System API

This is a system API.

Parameters

NameTypeMandatoryDescription
callbackAsyncCallback<BundleInstaller>YesCallback used to return the installation package.

Example

import bundle from '@ohos.bundle';

bundle.getBundleInstaller((err, data) => {
  if (err.code == 0) {
    console.error('getBundleInstaller failed.');
  } else {
    console.info('getBundleInstaller successfully');
  }
});

bundle.cleanBundleCacheFiles8+ deprecated

This API is deprecated since API version 9. You are advised to use bundleManager.cleanBundleCacheFiles instead.

cleanBundleCacheFiles(bundleName: string, callback: AsyncCallback<void>): void

Clears the cache data of an application. This API uses an asynchronous callback to return the result.

Required permissions

ohos.permission.REMOVE_CACHE_FILES

System capability

SystemCapability.BundleManager.BundleFramework

System API

This is a system API.

Parameters

NameTypeMandatoryDescription
bundleNamestringYesBundle name.
callbackAsyncCallback<void>YesCallback used to return the result.

Example

import bundle from '@ohos.bundle';

let bundleName: string = "com.example.myapplication";

bundle.cleanBundleCacheFiles(bundleName, err => {
  if (err) {
    console.error('cleanBundleCacheFiles failed.');
  } else {
    console.info('cleanBundleCacheFiles successfully.');
  }
});

bundle.cleanBundleCacheFiles8+ deprecated

This API is deprecated since API version 9. You are advised to use bundleManager.cleanBundleCacheFiles instead.

cleanBundleCacheFiles(bundleName: string): Promise<void>

Clears the cache data of an application. This API uses a promise to return the result.

Required permissions

ohos.permission.REMOVE_CACHE_FILES

System capability

SystemCapability.BundleManager.BundleFramework

System API

This is a system API.

Parameters

NameTypeMandatoryDescription
bundleNamestringYesBundle name.

Return value

TypeDescription
Promise<void>Promise that returns no value.

Example

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

let bundleName: string = "com.example.myapplication";

bundle.cleanBundleCacheFiles(bundleName).then(() => {
  console.info('cleanBundleCacheFiles successfully.');
}).catch((error: BusinessError) => {
  console.error('cleanBundleCacheFiles failed.');
});

bundle.setApplicationEnabled8+ deprecated

This API is deprecated since API version 9. You are advised to use bundleManager.setApplicationEnabled instead.

setApplicationEnabled(bundleName: string, isEnable: boolean, callback: AsyncCallback<void>): void

Sets whether to enable an application. This API uses an asynchronous callback to return the result.

Required permissions

ohos.permission.CHANGE_ABILITY_ENABLED_STATE

System capability

SystemCapability.BundleManager.BundleFramework

System API

This is a system API.

Parameters

NameTypeMandatoryDescription
bundleNamestringYesBundle name.
isEnablebooleanYesWhether to enable the application. The value true means to enable the application, and false means the opposite.
callbackAsyncCallback<void>YesCallback used to return the result.

Example

import bundle from '@ohos.bundle';

let bundleName: string = "com.example.myapplication";

bundle.setApplicationEnabled(bundleName, false, err => {
  if (err) {
    console.error('setApplicationEnabled failed.');
  } else {
    console.info('setApplicationEnabled successfully.');
  }
});

bundle.setApplicationEnabled8+ deprecated

This API is deprecated since API version 9. You are advised to use bundleManager.setApplicationEnabled instead.

setApplicationEnabled(bundleName: string, isEnable: boolean): Promise<void>

Sets whether to enable an application. This API uses a promise to return the result.

Required permissions

ohos.permission.CHANGE_ABILITY_ENABLED_STATE

System capability

SystemCapability.BundleManager.BundleFramework

System API

This is a system API.

Parameters

NameTypeMandatoryDescription
bundleNamestringYesBundle name.
isEnablebooleanYesWhether to enable the application. The value true means to enable the application, and false means the opposite.

Return value

TypeDescription
Promise<void>Promise that returns no value.

Example

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

let bundleName: string = "com.example.myapplication";

bundle.setApplicationEnabled(bundleName, false).then(() => {
  console.info('setApplicationEnabled successfully.');
}).catch((error: BusinessError) => {
  console.error('setApplicationEnabled failed.');
});

bundle.setAbilityEnabled8+ deprecated

This API is deprecated since API version 9. You are advised to use bundleManager.setAbilityEnabled instead.

setAbilityEnabled(info: AbilityInfo, isEnable: boolean, callback: AsyncCallback<void>): void

Sets whether to enable an ability. This API uses an asynchronous callback to return the result.

Required permissions

ohos.permission.CHANGE_ABILITY_ENABLED_STATE

System capability

SystemCapability.BundleManager.BundleFramework

System API

This is a system API.

Parameters

NameTypeMandatoryDescription
infoAbilityInfoYesAbility information.
isEnablebooleanYesWhether to enable the application. The value true means to enable the application, and false means the opposite.
callbackAsyncCallback<void>YesCallback used to return the result.

bundle.setAbilityEnabled8+ deprecated

This API is deprecated since API version 9. You are advised to use bundleManager.setAbilityEnabled instead.

setAbilityEnabled(info: AbilityInfo, isEnable: boolean): Promise<void>

Sets whether to enable an ability. This API uses a promise to return the result.

Required permissions

ohos.permission.CHANGE_ABILITY_ENABLED_STATE

System capability

SystemCapability.BundleManager.BundleFramework

System API

This is a system API.

Parameters

NameTypeMandatoryDescription
infoAbilityInfoYesAbility information.
isEnablebooleanYesWhether to enable the application. The value true means to enable the application, and false means the opposite.

Return value

TypeDescription
Promise<void>Promise that returns no value.

Example

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

let bundleName: string = "com.example.myapplication";
let abilityName: string = "EntryAbility";

bundle.getAbilityInfo(bundleName, abilityName).then((abilityInfo) => {
  console.info('getAbilityInfo successfully. Data: ' + JSON.stringify(abilityInfo));

  bundle.setAbilityEnabled(abilityInfo, false).then(data => {
    console.info('setAbilityEnabled successfully.');
  }).catch((error: BusinessError) => {
    console.error('setAbilityEnabled failed:' + JSON.stringify(error));
  })
}).catch((error: BusinessError) => {
  console.error('getAbilityInfo failed. Cause: ' + JSON.stringify(error));
});

bundle.getPermissionDef8+ deprecated

This API is deprecated since API version 9. You are advised to use bundleManager.getPermissionDef instead.

getPermissionDef(permissionName: string, callback: AsyncCallback<PermissionDef>): void

Obtains the permission details by permission name. This API uses an asynchronous callback to return the result.

Required permissions

ohos.permission.GET_BUNDLE_INFO_PRIVILEGED

System capability

SystemCapability.BundleManager.BundleFramework

System API

This is a system API.

Parameters

NameTypeMandatoryDescription
permissionNamestringYesName of the permission.
callbackAsyncCallback<PermissionDef>YesCallback used to return the permission details.

Example

import bundle from '@ohos.bundle';

let permission: string = "ohos.permission.GET_BUNDLE_INFO";
bundle.getPermissionDef(permission, (err, data) => {
  if (err) {
    console.error('getPermissionDef failed:' + err.message);
  } else {
    console.info('getPermissionDef successfully:' + JSON.stringify(data));
  }
});

bundle.getPermissionDef8+ deprecated

This API is deprecated since API version 9. You are advised to use bundleManager.getPermissionDef instead.

getPermissionDef(permissionName: string): Promise<PermissionDef>

Obtains the permission details by permission name. This API uses a promise to return the result.

Required permissions

ohos.permission.GET_BUNDLE_INFO_PRIVILEGED

System capability

SystemCapability.BundleManager.BundleFramework

System API

This is a system API.

Parameters

NameTypeMandatoryDescription
permissionNamestringYesName of the permission.

Return value

TypeDescription
Promise<PermissionDef>Promise used to return the permission details.

Example

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

let permissionName: string = "ohos.permission.GET_BUNDLE_INFO";
bundle.getPermissionDef(permissionName).then((data) => {
  console.info('getPermissionDef successfully. Data: ' + JSON.stringify(data));
}).catch((error: BusinessError) => {
  console.error('getPermissionDef failed. Cause: ' + error.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/XnGOUe