@ohos.enterprise.bundleManager(包管理)

本模块提供包管理能力,包括添加包安装白名单、获取包安装白名单、移除包安装白名单等。

说明:

本模块首批接口从API version 12开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。

本模块接口仅可在Stage模型下使用。

本模块接口仅对设备管理应用开放,需将设备管理应用激活后调用,实现相应功能。

导入模块

import { bundleManager } from '@kit.MDMKit';

bundleManager.addAllowedInstallBundlesSync

addAllowedInstallBundlesSync(admin: Want, appIds: Array<string>, accountId?: number): void

添加应用至包安装白名单,添加至白名单的应用允许在当前/指定用户下安装,否则不允许安装。

需要权限: ohos.permission.ENTERPRISE_SET_BUNDLE_INSTALL_POLICY

系统能力: SystemCapability.Customization.EnterpriseDeviceManager

参数:

参数名 类型 必填 说明
admin Want 企业设备管理扩展组件。
appIds Array<string> 应用ID数组。
accountId number 用户ID,取值范围:大于等于0。
accountId可以通过@ohos.account.osAccount中的getOsAccountLocalId等接口来获取。
- 调用接口时,若传入accountId,表示指定用户。
- 调用接口时,若未传入accountId,表示当前用户。

错误码

以下错误码的详细介绍请参见企业设备管理错误码通用错误码

错误码ID 错误信息
9200001 The application is not an administrator application of the device.
9200002 The administrator application does not have permission to manage the device.
201 Permission verification failed. The application does not have the permission required to call the API.
401 Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed.

示例:

import { Want } from '@kit.AbilityKit';
import { BusinessError } from '@kit.BasicServicesKit';
let wantTemp: Want = {
  bundleName: 'com.example.myapplication',
  abilityName: 'EntryAbility',
};
let appIds: Array<string> = ['com.example.******_******/******5t5CoBM='];

try {
  bundleManager.addAllowedInstallBundlesSync(wantTemp, appIds, 100);
  console.info('Succeeded in adding allowed install bundles.');
} catch (err) {
  console.error(`Failed to add allowed install bundles. Code is ${err.code}, message is ${err.message}`);
}

bundleManager.removeAllowedInstallBundlesSync

removeAllowedInstallBundlesSync(admin: Want, appIds: Array<string>, accountId?: number): void

在包安装白名单中移除应用,在白名单存在的情况下,不在包安装白名单中的应用不允许在当前/指定用户下安装。

需要权限: ohos.permission.ENTERPRISE_SET_BUNDLE_INSTALL_POLICY

系统能力: SystemCapability.Customization.EnterpriseDeviceManager

参数:

参数名 类型 必填 说明
admin Want 企业设备管理扩展组件。
appIds Array<string> 应用ID数组。
accountId number 用户ID,取值范围:大于等于0。
accountId可以通过@ohos.account.osAccount中的getOsAccountLocalId等接口来获取。
- 调用接口时,若传入accountId,表示指定用户。
- 调用接口时,若未传入accountId,表示当前用户。

错误码

以下错误码的详细介绍请参见企业设备管理错误码通用错误码

错误码ID 错误信息
9200001 The application is not an administrator application of the device.
9200002 The administrator application does not have permission to manage the device.
201 Permission verification failed. The application does not have the permission required to call the API.
401 Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed.

示例:

import { Want } from '@kit.AbilityKit';
import { BusinessError } from '@kit.BasicServicesKit';
let wantTemp: Want = {
  bundleName: 'com.example.myapplication',
  abilityName: 'EntryAbility',
};
let appIds: Array<string> = ['com.example.******_******/******5t5CoBM='];

try {
  bundleManager.removeAllowedInstallBundlesSync(wantTemp, appIds, 100);
  console.info('Succeeded in removing allowed install bundles.');
} catch (err) {
  console.error(`Failed to remove allowed install bundles. Code is ${err.code}, message is ${err.message}`);
}

bundleManager.getAllowedInstallBundlesSync

getAllowedInstallBundlesSync(admin: Want, accountId?: number): Array<string>

获取当前/指定用户下的包安装白名单。

需要权限: ohos.permission.ENTERPRISE_SET_BUNDLE_INSTALL_POLICY

系统能力: SystemCapability.Customization.EnterpriseDeviceManager

参数:

参数名 类型 必填 说明
admin Want 企业设备管理扩展组件。
accountId number 用户ID,取值范围:大于等于0。
accountId可以通过@ohos.account.osAccount中的getOsAccountLocalId等接口来获取。
- 调用接口时,若传入accountId,表示指定用户。
- 调用接口时,若未传入accountId,表示当前用户。

返回值:

类型 说明
Array<string> 返回当前用户下的包安装白名单。

错误码

以下错误码的详细介绍请参见企业设备管理错误码通用错误码

错误码ID 错误信息
9200001 The application is not an administrator application of the device.
9200002 The administrator application does not have permission to manage the device.
201 Permission verification failed. The application does not have the permission required to call the API.
401 Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed.

示例:

import { Want } from '@kit.AbilityKit';
let wantTemp: Want = {
  bundleName: 'com.example.myapplication',
  abilityName: 'EntryAbility',
};

try {
  let result: Array<string> = bundleManager.getAllowedInstallBundlesSync(wantTemp, 100);
  console.info(`Succeeded in getting allowed install bundles, result : ${JSON.stringify(result)}`);
} catch (err) {
  console.error(`Failed to get allowed install bundles. Code is ${err.code}, message is ${err.message}`);
}

bundleManager.addDisallowedInstallBundlesSync

addDisallowedInstallBundlesSync(admin: Want, appIds: Array<string>, accountId?: number): void

添加应用至包安装黑名单,添加至黑名单的应用不允许在当前/指定用户下安装。

需要权限: ohos.permission.ENTERPRISE_SET_BUNDLE_INSTALL_POLICY

系统能力: SystemCapability.Customization.EnterpriseDeviceManager

参数:

参数名 类型 必填 说明
admin Want 企业设备管理扩展组件。
appIds Array<string> 应用ID数组。
accountId number 用户ID,取值范围:大于等于0。
accountId可以通过@ohos.account.osAccount中的getOsAccountLocalId等接口来获取。
- 调用接口时,若传入accountId,表示指定用户。
- 调用接口时,若未传入accountId,表示当前用户。

错误码

以下错误码的详细介绍请参见企业设备管理错误码通用错误码

错误码ID 错误信息
9200001 The application is not an administrator application of the device.
9200002 The administrator application does not have permission to manage the device.
201 Permission verification failed. The application does not have the permission required to call the API.
401 Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed.

示例:

import { Want } from '@kit.AbilityKit';
import { BusinessError } from '@kit.BasicServicesKit';
let wantTemp: Want = {
  bundleName: 'com.example.myapplication',
  abilityName: 'EntryAbility',
};
let appIds: Array<string> = ['com.example.******_******/******5t5CoBM='];

try {
  bundleManager.addDisallowedInstallBundlesSync(wantTemp, appIds, 100);
  console.info('Succeeded in adding disallowed install bundles.');
} catch (err) {
  console.error(`Failed to add disallowed install bundles. Code is ${err.code}, message is ${err.message}`);
}

bundleManager.removeDisallowedInstallBundlesSync

removeDisallowedInstallBundlesSync(admin: Want, appIds: Array<string>, accountId?: number): void

在包安装黑名单中移除应用,在黑名单存在的情况下,在包安装黑名单中的应用不允许在当前/指定用户下安装。

需要权限: ohos.permission.ENTERPRISE_SET_BUNDLE_INSTALL_POLICY

系统能力: SystemCapability.Customization.EnterpriseDeviceManager

参数:

参数名 类型 必填 说明
admin Want 企业设备管理扩展组件。
appIds Array<string> 应用ID数组。
accountId number 用户ID,取值范围:大于等于0。
accountId可以通过@ohos.account.osAccount中的getOsAccountLocalId等接口来获取。
- 调用接口时,若传入accountId,表示指定用户。
- 调用接口时,若未传入accountId,表示当前用户。

错误码

以下错误码的详细介绍请参见企业设备管理错误码通用错误码

错误码ID 错误信息
9200001 The application is not an administrator application of the device.
9200002 The administrator application does not have permission to manage the device.
201 Permission verification failed. The application does not have the permission required to call the API.
401 Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed.

示例:

import { Want } from '@kit.AbilityKit';
import { BusinessError } from '@kit.BasicServicesKit';
let wantTemp: Want = {
  bundleName: 'com.example.myapplication',
  abilityName: 'EntryAbility',
};
let appIds: Array<string> = ['com.example.******_******/******5t5CoBM='];

try {
  bundleManager.removeDisallowedInstallBundlesSync(wantTemp, appIds, 100)
  console.info('Succeeded in removing disallowed install bundles.');
} catch (err) {
  console.error(`Failed to remove disallowed install bundles. Code is ${err.code}, message is ${err.message}`);
}

bundleManager.getDisallowedInstallBundlesSync

getDisallowedInstallBundlesSync(admin: Want, accountId?: number): Array<string>

获取当前/指定用户下的包安装黑名单。

需要权限: ohos.permission.ENTERPRISE_SET_BUNDLE_INSTALL_POLICY

系统能力: SystemCapability.Customization.EnterpriseDeviceManager

参数:

参数名 类型 必填 说明
admin Want 企业设备管理扩展组件。
accountId number 用户ID,取值范围:大于等于0。
accountId可以通过@ohos.account.osAccount中的getOsAccountLocalId等接口来获取。
- 调用接口时,若传入accountId,表示指定用户。
- 调用接口时,若未传入accountId,表示当前用户。

返回值:

类型 说明
Array<string> 返回当前用户下的包安装黑名单。

错误码

以下错误码的详细介绍请参见企业设备管理错误码通用错误码

错误码ID 错误信息
9200001 The application is not an administrator application of the device.
9200002 The administrator application does not have permission to manage the device.
201 Permission verification failed. The application does not have the permission required to call the API.
401 Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed.

示例:

import { Want } from '@kit.AbilityKit';
let wantTemp: Want = {
  bundleName: 'com.example.myapplication',
  abilityName: 'EntryAbility',
};

try {
  let result: Array<string> = bundleManager.getDisallowedInstallBundlesSync(wantTemp, 100);
  console.info(`Succeeded in getting disallowed install bundles, result : ${JSON.stringify(result)}`);
} catch (err) {
  console.error(`Failed to get disallowed install bundles. Code is ${err.code}, message is ${err.message}`);
}

bundleManager.addDisallowedUninstallBundlesSync

addDisallowedUninstallBundlesSync(admin: Want, appIds: Array<string>, accountId?: number): void

添加应用至包卸载黑名单,添加至黑名单的应用不允许在当前/指定用户下卸载。

需要权限: ohos.permission.ENTERPRISE_SET_BUNDLE_INSTALL_POLICY

系统能力: SystemCapability.Customization.EnterpriseDeviceManager

参数:

参数名 类型 必填 说明
admin Want 企业设备管理扩展组件。
appIds Array<string> 应用ID数组。
accountId number 用户ID,取值范围:大于等于0。
accountId可以通过@ohos.account.osAccount中的getOsAccountLocalId等接口来获取。
- 调用接口时,若传入accountId,表示指定用户。
- 调用接口时,若未传入accountId,表示当前用户。

错误码

以下错误码的详细介绍请参见企业设备管理错误码通用错误码

错误码ID 错误信息
9200001 The application is not an administrator application of the device.
9200002 The administrator application does not have permission to manage the device.
201 Permission verification failed. The application does not have the permission required to call the API.
401 Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed.

示例:

import { Want } from '@kit.AbilityKit';
let wantTemp: Want = {
  bundleName: 'com.example.myapplication',
  abilityName: 'EntryAbility',
};
let appIds: Array<string> = ['com.example.******_******/******5t5CoBM='];

try {
  bundleManager.addDisallowedUninstallBundlesSync(wantTemp, appIds, 100);
  console.info('Succeeded in adding disallowed uninstall bundles.');
} catch (err) {
  console.error(`Failed to add disallowed uninstall bundles. Code is ${err.code}, message is ${err.message}`);
}

bundleManager.removeDisallowedUninstallBundlesSync

removeDisallowedUninstallBundlesSync(admin: Want, appIds: Array<string>, accountId?: number): void

在包卸载黑名单中移除应用。在黑名单存在的情况下,在包卸载黑名单中的应用不允许在当前/指定用户下卸载。

需要权限: ohos.permission.ENTERPRISE_SET_BUNDLE_INSTALL_POLICY

系统能力: SystemCapability.Customization.EnterpriseDeviceManager

参数:

参数名 类型 必填 说明
admin Want 企业设备管理扩展组件。
appIds Array<string> 应用ID数组。
accountId number 用户ID,取值范围:大于等于0。
accountId可以通过@ohos.account.osAccount中的getOsAccountLocalId等接口来获取。
- 调用接口时,若传入accountId,表示指定用户。
- 调用接口时,若未传入accountId,表示当前用户。

错误码

以下错误码的详细介绍请参见企业设备管理错误码通用错误码

错误码ID 错误信息
9200001 The application is not an administrator application of the device.
9200002 The administrator application does not have permission to manage the device.
201 Permission verification failed. The application does not have the permission required to call the API.
401 Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed.

示例:

import { Want } from '@kit.AbilityKit';
let wantTemp: Want = {
  bundleName: 'com.example.myapplication',
  abilityName: 'EntryAbility',
};
let appIds: Array<string> = ['com.example.******_******/******5t5CoBM='];

try {
  bundleManager.removeDisallowedUninstallBundlesSync(wantTemp, appIds, 100);
  console.info('Succeeded in removing disallowed uninstall bundles.');
} catch (err) {
  console.error(`Failed to remove disallowed uninstall bundles. Code is ${err.code}, message is ${err.message}`);
}

bundleManager.getDisallowedUninstallBundlesSync

getDisallowedUninstallBundlesSync(admin: Want, accountId?: number): Array<string>

获取当前/指定用户下包卸载黑名单接口。

需要权限: ohos.permission.ENTERPRISE_SET_BUNDLE_INSTALL_POLICY

系统能力: SystemCapability.Customization.EnterpriseDeviceManager

参数:

参数名 类型 必填 说明
admin Want 企业设备管理扩展组件。
accountId number 用户ID,取值范围:大于等于0。
accountId可以通过@ohos.account.osAccount中的getOsAccountLocalId等接口来获取。
- 调用接口时,若传入accountId,表示指定用户。
- 调用接口时,若未传入accountId,表示当前用户。

返回值:

类型 说明
Array<string> 返回当前用户下的包卸载黑名单。

错误码

以下错误码的详细介绍请参见企业设备管理错误码通用错误码

错误码ID 错误信息
9200001 The application is not an administrator application of the device.
9200002 The administrator application does not have permission to manage the device.
201 Permission verification failed. The application does not have the permission required to call the API.
401 Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed.

示例:

import { Want } from '@kit.AbilityKit';
let wantTemp: Want = {
  bundleName: 'com.example.myapplication',
  abilityName: 'EntryAbility',
};

try {
  let result: Array<String> = bundleManager.getDisallowedUninstallBundlesSync(wantTemp, 100);
  console.info(`Succeeded in getting disallowed uninstall bundles, result : ${JSON.stringify(result)}`);
} catch (err) {
  console.error(`Failed to get disallowed uninstall bundles. Code is ${err.code}, message is ${err.message}`);
}

bundleManager.uninstall

uninstall(admin: Want, bundleName: string, userId?: number, isKeepData?: boolean): Promise<void>

卸载当前/指定用户下的指定包接口,选择是否保留包数据(由isKeepData指定)。使用promise异步回调。

需要权限: ohos.permission.ENTERPRISE_INSTALL_BUNDLE

系统能力: SystemCapability.Customization.EnterpriseDeviceManager

参数:

参数名 类型 必填 说明
admin Want 企业设备管理扩展组件。
bundleName string 包名。
userId number 用户ID,取值范围:大于等于0。
- 调用接口时,若传入userId,表示指定用户。
- 调用接口时,若未传入userId,表示当前用户。
isKeepData boolean 是否保留包数据,true表示保留,false表示不保留。

返回值:

类型 说明
Promise<void> 无返回结果的Promise对象。当包卸载失败时抛出错误对象。

错误码

以下错误码的详细介绍请参见企业设备管理错误码通用错误码

错误码ID 错误信息
9200001 The application is not an administrator application of the device.
9200002 The administrator application does not have permission to manage the device.
201 Permission verification failed. The application does not have the permission required to call the API.
401 Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed.

示例:

import { Want } from '@kit.AbilityKit';
import { BusinessError } from '@kit.BasicServicesKit';
let wantTemp: Want = {
  bundleName: 'com.example.myapplication',
  abilityName: 'EntryAbility',
};

bundleManager.uninstall(wantTemp, 'bundleName', 100, true).then(() => {
  console.info('Succeeded in uninstalling bundles.');
}).catch((err: BusinessError) => {
  console.error(`Failed to uninstall bundles. Code is ${err.code}, message is ${err.message}`);
});

bundleManager.install

install(admin: Want, hapFilePaths: Array<string>, installParam?: InstallParam): Promise<void>

安装指定路径下的应用包。使用promise异步回调。 注意:此接口只能安装分发类型为enterprise_mdm和enterprise_normal类型的应用。

需要权限: ohos.permission.ENTERPRISE_INSTALL_BUNDLE

系统能力: SystemCapability.Customization.EnterpriseDeviceManager

参数:

参数名 类型 必填 说明
admin Want 企业设备管理扩展组件。
hapFilePaths Array<string> 待安装应用包路径数组。
installParam InstallParam 应用包安装参数。

返回值:

类型 说明
Promise<void> 无返回结果的Promise对象。当包安装失败时,抛出错误对象。

错误码

以下错误码的详细介绍请参见企业设备管理错误码通用错误码

错误码ID 错误信息
9200001 The application is not an administrator application of the device.
9200002 The administrator application does not have permission to manage the device.
9201002 Failed to install the application.
201 Permission verification failed. The application does not have the permission required to call the API.
401 Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed.

示例:

import { Want } from '@kit.AbilityKit';
import { BusinessError } from '@kit.BasicServicesKit';
let wantTemp: Want = {
  bundleName: 'com.example.myapplication',
  abilityName: 'EntryAbility',
};
let hapFilePaths: Array<string> = ['/data/storage/el2/base/haps/entry/testinstall/ExtensionTest.hap'];

bundleManager.install(wantTemp, hapFilePaths).then(() => {
  console.info('Succeeded in installing bundles.');
}).catch((err: BusinessError) => {
  console.error(`Failed to install bundles. Code is ${err.code}, message is ${err.message}`);
});

InstallParam

应用包安装需指定的参数信息。

系统能力: SystemCapability.Customization.EnterpriseDeviceManager

名称 类型 必填 说明
userId number 指示用户id,默认值:调用方所在用户,取值范围:大于等于0。
installFlag number 安装标志。枚举值:0:应用初次安装,1:应用覆盖安装,2:应用免安装,默认值为应用初次安装。