1# @ohos.bundle (Bundle模块)(系统接口) 2 3本模块提供应用信息查询能力,支持[包信息](js-apis-bundle-BundleInfo.md)、[应用信息](js-apis-bundle-ApplicationInfo.md)、[Ability组件信息](js-apis-bundle-AbilityInfo.md)等信息的查询,以及应用禁用状态的查询、设置等。 4 5> **说明:** 6> 7> 本模块首批接口从API version 7开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 8> 9> 从API Version 9开始,该模块不再维护,系统接口建议使用[`@ohos.bundle.bundleManager`](js-apis-bundleManager-sys.md)替代。 10> 11> 当前页面仅包含本模块的系统接口,其他公开接口参见[`@ohos.bundle`](js-apis-Bundle.md)。 12 13## 导入模块 14 15```ts 16import bundle from '@ohos.bundle'; 17``` 18 19## 权限列表 20 21| 权限 | 权限等级 | 描述 | 22|--------------------------------------------|--------------|---------------| 23| ohos.permission.CHANGE_ABILITY_ENABLED_STATE | system_basic | 设置禁用使能所需的权限。 | 24| ohos.permission.GET_BUNDLE_INFO | normal | 查询指定应用信息。 | 25| ohos.permission.GET_BUNDLE_INFO_PRIVILEGED | system_basic | 可查询所有应用信息。 | 26| ohos.permission.INSTALL_BUNDLE | system_core | 可安装、卸载应用。 | 27| ohos.permission.REMOVE_CACHE_FILES | system_basic | 清理应用缓存。 | 28 29权限等级参考[权限APL等级说明](../../security/AccessToken/app-permission-mgmt-overview.md#权限机制中的基本概念)。 30 31## bundle.getBundleInstaller<sup>deprecated<sup> 32 33> 从API version 9开始不再维护,建议使用[installer.getBundleInstaller](js-apis-installer-sys.md#bundleinstallergetbundleinstaller)替代。 34 35getBundleInstaller(): Promise<BundleInstaller> 36 37获取用于安装包的接口,使用Promise异步回调,返回安装接口对象。 38 39**需要权限:** 40 41ohos.permission.INSTALL_BUNDLE 42 43**系统能力:** 44 45SystemCapability.BundleManager.BundleFramework 46 47**系统接口:** 48 49此接口为系统接口。 50 51**返回值:** 52 53| 类型 | 说明 | 54| ------------------------------------------------------------ | -------------------------------------------- | 55| Promise<[BundleInstaller](js-apis-bundle-BundleInstaller-sys.md)> | Promise对象,返回安装接口对象。 | 56 57**示例:** 58 59```ts 60import bundle from '@ohos.bundle'; 61import { BusinessError } from '@ohos.base'; 62 63bundle.getBundleInstaller().then((data) => { 64 console.info('getBundleInstaller successfully.'); 65}).catch((error: BusinessError) => { 66 console.error('getBundleInstaller failed.'); 67}); 68``` 69 70## bundle.getBundleInstaller<sup>deprecated<sup> 71 72> 从API version 9开始不再维护,建议使用[installer.getBundleInstaller](js-apis-installer-sys.md#bundleinstallergetbundleinstaller)替代。 73 74getBundleInstaller(callback: AsyncCallback<BundleInstaller>): void 75 76获取用于安装包的接口,使用callback异步回调。 77 78**需要权限:** 79 80ohos.permission.INSTALL_BUNDLE 81 82**系统能力:** 83 84SystemCapability.BundleManager.BundleFramework 85 86**系统接口:** 87 88此接口为系统接口。 89 90**参数:** 91 92| 参数名 | 类型 | 必填 | 说明 | 93| -------- | ------------------------------------------------------------ | ---- | ---------------- | 94| callback | AsyncCallback<[BundleInstaller](js-apis-bundle-BundleInstaller-sys.md)> | 是 | 回调函数,返回安装接口对象。 | 95 96**示例:** 97 98```ts 99import bundle from '@ohos.bundle'; 100 101bundle.getBundleInstaller((err, data) => { 102 if (err.code == 0) { 103 console.error('getBundleInstaller failed.'); 104 } else { 105 console.info('getBundleInstaller successfully'); 106 } 107}); 108``` 109## bundle.cleanBundleCacheFiles<sup>8+</sup> <sup>deprecated<sup> 110 111> 从API version 9开始不再维护,建议使用[bundleManager.cleanBundleCacheFiles](js-apis-bundleManager-sys.md#bundlemanagercleanbundlecachefiles)替代。 112 113cleanBundleCacheFiles(bundleName: string, callback: AsyncCallback<void>): void 114 115清除指定应用程序的缓存数据,使用callback异步回调。 116 117**需要权限:** 118 119ohos.permission.REMOVE_CACHE_FILES 120 121**系统能力:** 122 123SystemCapability.BundleManager.BundleFramework 124 125**系统接口:** 126 127此接口为系统接口。 128 129**参数:** 130 131| 参数名 | 类型 | 必填 | 说明 | 132| ---------- | ------------------- | ---- | ------------------------------------- | 133| bundleName | string | 是 | 指示要清除其缓存数据的应用Bundle名称。 | 134| callback | AsyncCallback\<void> | 是 | 回调函数。 | 135 136**示例:** 137 138```ts 139import bundle from '@ohos.bundle'; 140 141let bundleName: string = "com.example.myapplication"; 142 143bundle.cleanBundleCacheFiles(bundleName, err => { 144 if (err) { 145 console.error('cleanBundleCacheFiles failed.'); 146 } else { 147 console.info('cleanBundleCacheFiles successfully.'); 148 } 149}); 150``` 151 152## bundle.cleanBundleCacheFiles<sup>8+</sup> <sup>deprecated<sup> 153 154> 从API version 9开始不再维护,建议使用[bundleManager.cleanBundleCacheFiles](js-apis-bundleManager-sys.md#bundlemanagercleanbundlecachefiles)替代。 155 156cleanBundleCacheFiles(bundleName: string): Promise<void> 157 158清除指定应用程序的缓存数据,使用Promise异步回调。 159 160**需要权限:** 161 162ohos.permission.REMOVE_CACHE_FILES 163 164**系统能力:** 165 166SystemCapability.BundleManager.BundleFramework 167 168**系统接口:** 169 170此接口为系统接口。 171 172**参数:** 173 174| 参数名 | 类型 | 必填 | 说明 | 175| ---------- | ------ | ---- | ------------------------------------- | 176| bundleName | string | 是 | 指示要清除其缓存数据的应用Bundle名称。 | 177 178**返回值:** 179 180| 类型 | 说明 | 181| ------------- | ------------------------------------ | 182| Promise\<void> | Promise对象,无返回结果的Promise对象。 | 183 184**示例:** 185 186```ts 187import bundle from '@ohos.bundle'; 188import { BusinessError } from '@ohos.base'; 189 190let bundleName: string = "com.example.myapplication"; 191 192bundle.cleanBundleCacheFiles(bundleName).then(() => { 193 console.info('cleanBundleCacheFiles successfully.'); 194}).catch((error: BusinessError) => { 195 console.error('cleanBundleCacheFiles failed.'); 196}); 197``` 198 199## bundle.setApplicationEnabled<sup>8+</sup> <sup>deprecated<sup> 200 201> 从API version 9开始不再维护,建议使用[bundleManager.setApplicationEnabled](js-apis-bundleManager-sys.md#bundlemanagersetapplicationenabled)替代。 202 203setApplicationEnabled(bundleName: string, isEnable: boolean, callback: AsyncCallback<void>): void 204 205设置是否启用指定的应用程序,使用callback异步回调。 206 207**需要权限:** 208 209ohos.permission.CHANGE_ABILITY_ENABLED_STATE 210 211**系统能力:** 212 213SystemCapability.BundleManager.BundleFramework 214 215**系统接口:** 216 217此接口为系统接口。 218 219**参数:** 220 221| 参数名 | 类型 | 必填 | 说明 | 222| ---------- | ------------------- | ---- |--------------------------------| 223| bundleName | string | 是 | 指示需要启用或禁用的应用Bundle名称。 | 224| isEnable | boolean | 是 | 指定是否启用应用程序。true表示启用,false表示禁用。 | 225| callback | AsyncCallback\<void> | 是 | 回调函数。 | 226 227**示例:** 228 229```ts 230import bundle from '@ohos.bundle'; 231 232let bundleName: string = "com.example.myapplication"; 233 234bundle.setApplicationEnabled(bundleName, false, err => { 235 if (err) { 236 console.error('setApplicationEnabled failed.'); 237 } else { 238 console.info('setApplicationEnabled successfully.'); 239 } 240}); 241``` 242 243## bundle.setApplicationEnabled<sup>8+</sup> <sup>deprecated<sup> 244 245> 从API version 9开始不再维护,建议使用[bundleManager.setApplicationEnabled](js-apis-bundleManager-sys.md#bundlemanagersetapplicationenabled)替代。 246 247setApplicationEnabled(bundleName: string, isEnable: boolean): Promise<void> 248 249设置是否启用指定的应用程序,使用Promise异步回调。 250 251**需要权限:** 252 253ohos.permission.CHANGE_ABILITY_ENABLED_STATE 254 255**系统能力:** 256 257SystemCapability.BundleManager.BundleFramework 258 259**系统接口:** 260 261此接口为系统接口。 262 263**参数:** 264 265| 参数名 | 类型 | 必填 | 说明 | 266| ---------- | ------- | ---- | ----------------------------------------------- | 267| bundleName | string | 是 | 指示需要启用或禁用的应用Bundle名称。 | 268| isEnable | boolean | 是 | 指定是否启用应用程序。true表示启用,false禁用。 | 269 270**返回值:** 271 272| 类型 | 说明 | 273| ------------- | ------------------------------------ | 274| Promise\<void> | Promise对象,无返回结果的Promise对象。 | 275 276**示例:** 277 278```ts 279import bundle from '@ohos.bundle'; 280import { BusinessError } from '@ohos.base'; 281 282let bundleName: string = "com.example.myapplication"; 283 284bundle.setApplicationEnabled(bundleName, false).then(() => { 285 console.info('setApplicationEnabled successfully.'); 286}).catch((error: BusinessError) => { 287 console.error('setApplicationEnabled failed.'); 288}); 289``` 290 291## bundle.setAbilityEnabled<sup>8+</sup> <sup>deprecated<sup> 292 293> 从API version 9开始不再维护,建议使用[bundleManager.setAbilityEnabled](js-apis-bundleManager-sys.md#bundlemanagersetabilityenabled)替代。 294 295setAbilityEnabled(info: AbilityInfo, isEnable: boolean, callback: AsyncCallback<void>): void 296 297设置是否启用指定的Ability组件,使用callback异步回调。 298 299**需要权限:** 300 301ohos.permission.CHANGE_ABILITY_ENABLED_STATE 302 303**系统能力:** 304 305SystemCapability.BundleManager.BundleFramework 306 307**系统接口:** 308 309此接口为系统接口。 310 311**参数:** 312 313| 参数名 | 类型 | 必填 | 说明 | 314| -------- | -------------------------------------------- | ---- | ----------------------------------------------- | 315| info | [AbilityInfo](js-apis-bundle-AbilityInfo.md) | 是 | Ability信息,指示需要设置启用状态的Ability。 | 316| isEnable | boolean | 是 | 指定是否启用应用程序。true表示启用,false禁用。 | 317| callback | AsyncCallback\<void> | 是 | 为返回操作结果而调用的回调。 | 318 319## bundle.setAbilityEnabled<sup>8+</sup> <sup>deprecated<sup> 320 321> 从API version 9开始不再维护,建议使用[bundleManager.setAbilityEnabled](js-apis-bundleManager-sys.md#bundlemanagersetabilityenabled)替代。 322 323setAbilityEnabled(info: AbilityInfo, isEnable: boolean): Promise<void> 324 325设置是否启用指定的Ability组件,使用Promise异步回调。 326 327**需要权限:** 328 329ohos.permission.CHANGE_ABILITY_ENABLED_STATE 330 331**系统能力:** 332 333SystemCapability.BundleManager.BundleFramework 334 335**系统接口:** 336 337此接口为系统接口。 338 339**参数:** 340 341| 参数名 | 类型 | 必填 | 说明 | 342| -------- | -------------------------------------------- | ---- | ----------------------------------------------- | 343| info | [AbilityInfo](js-apis-bundle-AbilityInfo.md) | 是 | Ability信息,指示需要设置启用状态的Ability。 | 344| isEnable | boolean | 是 | 指定是否启用应用程序。true表示启用,false禁用。 | 345 346**返回值:** 347 348| 类型 | 说明 | 349| ------------- | ------------------------------------ | 350| Promise\<void> | Promise对象,无返回结果的Promise对象。 | 351 352**示例:** 353 354```ts 355import bundle from '@ohos.bundle'; 356import { BusinessError } from '@ohos.base'; 357 358let bundleName: string = "com.example.myapplication"; 359let abilityName: string = "EntryAbility"; 360 361bundle.getAbilityInfo(bundleName, abilityName).then((abilityInfo) => { 362 console.info('getAbilityInfo successfully. Data: ' + JSON.stringify(abilityInfo)); 363 364 bundle.setAbilityEnabled(abilityInfo, false).then(data => { 365 console.info('setAbilityEnabled successfully.'); 366 }).catch((error: BusinessError) => { 367 console.error('setAbilityEnabled failed:' + JSON.stringify(error)); 368 }) 369}).catch((error: BusinessError) => { 370 console.error('getAbilityInfo failed. Cause: ' + JSON.stringify(error)); 371}); 372``` 373## bundle.getPermissionDef<sup>8+</sup> <sup>deprecated<sup> 374 375> 从API version 9开始不再维护,建议使用[bundleManager.getPermissionDef](js-apis-bundleManager-sys.md#bundlemanagergetpermissiondef)替代。 376 377getPermissionDef(permissionName: string, callback: AsyncCallback<PermissionDef>): void 378 379按权限名称获取权限的详细信息,使用callback异步回调。 380 381**需要权限:** 382 383ohos.permission.GET_BUNDLE_INFO_PRIVILEGED 384 385**系统能力:** 386 387SystemCapability.BundleManager.BundleFramework 388 389**系统接口:** 390 391此接口为系统接口。 392 393**参数:** 394 395| 参数名 | 类型 | 必填 | 说明 | 396| -------------- | ------------------------------------------------------------ | ---- | ------------------------------------------------ | 397| permissionName | string | 是 | 需要查询的权限的名称。 | 398| callback | AsyncCallback<[PermissionDef](js-apis-bundle-PermissionDef-sys.md)> | 是 | 程序启动作为入参的回调函数,返回定义的权限信息。 | 399 400**示例:** 401 402```ts 403import bundle from '@ohos.bundle'; 404 405let permission: string = "ohos.permission.GET_BUNDLE_INFO"; 406bundle.getPermissionDef(permission, (err, data) => { 407 if (err) { 408 console.error('getPermissionDef failed:' + err.message); 409 } else { 410 console.info('getPermissionDef successfully:' + JSON.stringify(data)); 411 } 412}); 413``` 414 415## bundle.getPermissionDef<sup>8+</sup> <sup>deprecated<sup> 416 417> 从API version 9开始不再维护,建议使用[bundleManager.getPermissionDef](js-apis-bundleManager-sys.md#bundlemanagergetpermissiondef)替代。 418 419getPermissionDef(permissionName: string): Promise<PermissionDef> 420 421按权限名称获取权限的详细信息,使用promise异步回调。。 422 423**需要权限:** 424 425ohos.permission.GET_BUNDLE_INFO_PRIVILEGED 426 427**系统能力:** 428 429SystemCapability.BundleManager.BundleFramework 430 431**系统接口:** 432 433此接口为系统接口。 434 435**参数:** 436 437| 参数名 | 类型 | 必填 | 说明 | 438| -------------- | ------ | ---- | ---------------- | 439| permissionName | string | 是 | 需要查询的权限的名称。 | 440 441**返回值:** 442 443| 类型 | 说明 | 444| ------------------------------------------------------ | ------------------------------------------------------ | 445| Promise<[PermissionDef](js-apis-bundle-PermissionDef-sys.md)> | Promise对象,获取成功时返回权限详细信息。 | 446 447**示例:** 448 449```ts 450import bundle from '@ohos.bundle'; 451import { BusinessError } from '@ohos.base'; 452 453let permissionName: string = "ohos.permission.GET_BUNDLE_INFO"; 454bundle.getPermissionDef(permissionName).then((data) => { 455 console.info('getPermissionDef successfully. Data: ' + JSON.stringify(data)); 456}).catch((error: BusinessError) => { 457 console.error('getPermissionDef failed. Cause: ' + error.message); 458}); 459```