1# @ohos.bundle.freeInstall (freeInstall模块) 2 3本模块提供免安装相关的设置和查询能力,支持BundlePackInfo、DispatchInfo等信息的查询 4 5> **说明:** 6> 7> 本模块首批接口从API version 9开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 8 9本模块接口为系统接口。 10 11## 导入模块 12 13```js 14import freeInstall from '@ohos.bundle.freeInstall'; 15``` 16 17## 权限列表 18 19| 权限 | 权限等级 | 描述 | 20| ------------------------------------------ | ------------ | ------------------ | 21| ohos.permission.GET_BUNDLE_INFO_PRIVILEGED | system_basic | 可查询所有应用信息。 | 22| ohos.permission.INSTALL_BUNDLE | system_core | 允许应用安装、卸载其他应用(除了企业相关应用,目前有企业InHouse应用,企业MDM应用和企业normal应用)。 | 23 24权限等级参考[权限等级说明](../../security/accesstoken-overview.md#权限等级说明)。 25## UpgradeFlag 26 27**系统接口:** 此接口为系统接口。 28 29**系统能力:** SystemCapability.BundleManager.BundleFramework.FreeInstall 30 31| 名称 | 值 | 说明 | 32| ---------------- | ---- | ---------------- | 33| NOT_UPGRADE | 0 | 模块无需升级。 | 34| SINGLE_UPGRADE | 1 | 单个模块需要升级。 | 35| RELATION_UPGRADE | 2 | 关系模块需要升级。 | 36 37## BundlePackFlag 38 39**系统接口:** 此接口为系统接口。 40 41**系统能力:** SystemCapability.BundleManager.BundleFramework.FreeInstall 42 43| 名称 | 值 | 说明 | 44| ------------------ | ---------- | ---------------------------------- | 45| GET_PACK_INFO_ALL | 0x00000000 | 获取应用包pack.info的所有信息。 | 46| GET_PACKAGES | 0x00000001 | 获取应用包pack.info的package信息。 | 47| GET_BUNDLE_SUMMARY | 0x00000002 | 获取应用包pack.info的bundle摘要信息。 | 48| GET_MODULE_SUMMARY | 0x00000004 | 获取应用包pack.info的module摘要信息。 | 49 50## freeInstall.setHapModuleUpgradeFlag 51 52setHapModuleUpgradeFlag(bundleName: string, moduleName: string, upgradeFlag: UpgradeFlag, callback: AsyncCallback\<void>):void; 53 54设置指定模块是否升级。使用callback异步回调。 55 56**需要权限:** ohos.permission.INSTALL_BUNDLE 57 58**系统接口:** 此接口为系统接口。 59 60**系统能力:** SystemCapability.BundleManager.BundleFramework.FreeInstall 61 62**参数:** 63 64| 参数名 | 类型 | 必填 | 说明 | 65| ----------- | --------------------------- | ---- | ---------------------------- | 66| bundleName | string | 是 | 应用Bundle名称。 | 67| moduleName | string | 是 | 应用程序模块名称。 | 68| upgradeFlag | [UpgradeFlag](#upgradeflag) | 是 | 仅供内部系统使用标志位 | 69| callback | AsyncCallback\<void> | 是 | 回调函数。当函数调用成功,err为null,否则为错误对象。 | 70 71**错误码:** 72 73以下错误码的详细介绍请参见[ohos.bundle错误码](../errorcodes/errorcode-bundle.md)。 74 75| 错误码ID | 错误信息 | 76|----------|---------------------------------------- | 77| 17700001 | The specified bundle name is not found. | 78| 17700002 | The specified module name is not found. | 79 80**示例:** 81 82```js 83import freeInstall from '@ohos.bundle.freeInstall'; 84let bundleName = 'com.example.myapplication'; 85let moduleName = 'entry'; 86let upgradeFlag = freeInstall.UpgradeFlag.SINGLE_UPGRADE; 87try { 88 freeInstall.setHapModuleUpgradeFlag(bundleName, moduleName, upgradeFlag, err => { 89 if (err) { 90 console.error('Operation failed:' + JSON.stringify(err)); 91 } else { 92 console.info('Operation succeed'); 93 } 94 }); 95} catch (err) { 96 console.error('Operation failed:' + JSON.stringify(err)); 97} 98``` 99 100## setHapModuleUpgradeFlag 101 102setHapModuleUpgradeFlag(bundleName: string, moduleName: string, upgradeFlag: UpgradeFlag): Promise\<void>; 103 104设置指定模块是否升级。使用Promise异步回调。 105 106**系统接口:** 此接口为系统接口。 107 108**需要权限:** ohos.permission.INSTALL_BUNDLE 109 110**系统能力:** SystemCapability.BundleManager.BundleFramework.FreeInstall 111 112**参数:** 113 114| 参数名 | 类型 | 必填 | 说明 | 115| ----------- | --------------------------- | ---- | ---------------------- | 116| bundleName | string | 是 | 应用Bundle名称。 | 117| moduleName | string | 是 | 应用程序模块名称。 | 118| upgradeFlag | [UpgradeFlag](#upgradeflag) | 是 | 仅供内部系统使用标志位。| 119 120**返回值:** 121 122| 类型 | 说明 | 123| ------------- | ------------------------------------ | 124| Promise\<void> | Promise对象。无返回结果的Promise对象。 | 125 126**错误码:** 127 128以下错误码的详细介绍请参见[ohos.bundle错误码](../errorcodes/errorcode-bundle.md)。 129 130| 错误码ID | 错误信息 | 131|----------|----------------------------------------| 132| 17700001 | The specified bundle name is not found. | 133| 17700002 | The specified module name is not found. | 134 135**示例:** 136 137```js 138import freeInstall from '@ohos.bundle.freeInstall'; 139import { BusinessError } from '@ohos.base'; 140let bundleName = 'com.example.myapplication'; 141let moduleName = 'entry'; 142let upgradeFlag = freeInstall.UpgradeFlag.SINGLE_UPGRADE; 143try { 144 freeInstall.setHapModuleUpgradeFlag(bundleName, moduleName, upgradeFlag).then(() => { 145 console.info('Operation succeed') 146 }).catch((err: BusinessError) => { 147 console.error('Operation failed:' + JSON.stringify(err)); 148 }); 149} catch (err) { 150 console.error('Operation failed:' + JSON.stringify(err)); 151} 152``` 153 154## isHapModuleRemovable 155 156isHapModuleRemovable(bundleName: string, moduleName: string, callback: AsyncCallback\<boolean>): void; 157 158查询指定模块是否可以被移除。使用callback异步回调。 159 160**系统接口:** 此接口为系统接口。 161 162**需要权限:** ohos.permission.GET_BUNDLE_INFO_PRIVILEGED 163 164**系统能力:** SystemCapability.BundleManager.BundleFramework.FreeInstall 165 166**参数:** 167 168| 参数名 | 类型 | 必填 | 说明 | 169| ---------- | ---------------------- | ---- | --------------------------------------------- | 170| bundleName | string | 是 | 应用Bundle名称。 | 171| moduleName | string | 是 | 应用程序模块名称。 | 172| callback | AsyncCallback\<boolean> | 是 | 回调函数。返回true表示可以移除;返回false表示不可移除。 | 173 174**错误码:** 175 176以下错误码的详细介绍请参见[ohos.bundle错误码](../errorcodes/errorcode-bundle.md)。 177 178| 错误码ID | 错误信息 | 179|----------|----------------------------------------| 180| 17700001 | The specified bundle name is not found. | 181| 17700002 | The specified module name is not found. | 182 183**示例:** 184 185```js 186import freeInstall from '@ohos.bundle.freeInstall'; 187let bundleName = 'com.example.myapplication'; 188let moduleName = 'entry'; 189try { 190 freeInstall.isHapModuleRemovable(bundleName, moduleName, (err, data) => { 191 if (err) { 192 console.error('Operation failed:' + JSON.stringify(err)); 193 } else { 194 console.info('Operation succeed:' + JSON.stringify(data)); 195 } 196 }); 197} catch (err) { 198 console.error('Operation failed:' + JSON.stringify(err)); 199} 200``` 201 202## isHapModuleRemovable 203 204isHapModuleRemovable(bundleName: string, moduleName: string): Promise\<boolean>; 205 206查询指定模块是否可以被移除。使用Promise异步回调。 207 208**系统接口:** 此接口为系统接口。 209 210**需要权限:** ohos.permission.GET_BUNDLE_INFO_PRIVILEGED 211 212**系统能力:** SystemCapability.BundleManager.BundleFramework.FreeInstall 213 214**参数:** 215 216| 参数名 | 类型 | 必填 | 说明 | 217| ---------- | ------ | ---- | ------------------ | 218| bundleName | string | 是 | 应用Bundle名称。 | 219| moduleName | string | 是 | 应用程序模块名称。 | 220 221**返回值:** 222 223| 类型 | 说明 | 224| ---------------- | ---------------------------- | 225| Promise\<boolean> | Promise对象。返回true表示可以移除;返回false表示不可移除。 | 226 227**错误码:** 228 229以下错误码的详细介绍请参见[ohos.bundle错误码](../errorcodes/errorcode-bundle.md)。 230 231| 错误码ID | 错误信息 | 232|----------|----------------------------------------| 233| 17700001 | The specified bundle name is not found. | 234| 17700002 | The specified module name is not found. | 235 236**示例:** 237 238```js 239import freeInstall from '@ohos.bundle.freeInstall'; 240import { BusinessError } from '@ohos.base'; 241let bundleName = 'com.example.myapplication'; 242let moduleName = 'entry'; 243try { 244 freeInstall.isHapModuleRemovable(bundleName, moduleName).then(data => { 245 console.info('Operation succeed:' + JSON.stringify(data)); 246 }).catch((err: BusinessError) => { 247 console.error('Operation failed:' + JSON.stringify(err)); 248 }); 249} catch (err) { 250 console.error('Operation failed:' + JSON.stringify(err)); 251} 252``` 253 254## getBundlePackInfo 255 256getBundlePackInfo(bundleName: string, bundlePackFlag : BundlePackFlag, callback: AsyncCallback\<BundlePackInfo>): void; 257 258基于bundleName和bundlePackFlag来获取bundlePackInfo。使用callback异步回调。 259 260**系统接口:** 此接口为系统接口。 261 262**需要权限:** ohos.permission.GET_BUNDLE_INFO_PRIVILEGED 263 264**系统能力:** SystemCapability.BundleManager.BundleFramework.FreeInstall 265 266**参数:** 267 268| 参数名 | 类型 | 必填 | 说明 | 269| -------------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 270| bundleName | string | 是 | 应用Bundle名称。 | 271| bundlePackFlag | [BundlePackFlag](#bundlepackflag) | 是 | 指示要查询的应用包标志。 | 272| callback | AsyncCallback<[BundlePackInfo](js-apis-bundleManager-BundlePackInfo.md)> | 是 | 回调函数。当函数调用成功,err为null,data为获取到的BundlePackInfo信息。否则为错误对象。 | 273 274**错误码:** 275 276以下错误码的详细介绍请参见[ohos.bundle错误码](../errorcodes/errorcode-bundle.md)。 277 278| 错误码ID | 错误信息 | 279|----------|----------------------------------------| 280| 17700001 | The specified bundle name is not found. | 281 282**示例:** 283 284```js 285import freeInstall from '@ohos.bundle.freeInstall'; 286let bundleName = 'com.example.myapplication'; 287let bundlePackFlag = freeInstall.BundlePackFlag.GET_PACK_INFO_ALL; 288try { 289 freeInstall.getBundlePackInfo(bundleName, bundlePackFlag, (err, data) => { 290 if (err) { 291 console.error('Operation failed:' + JSON.stringify(err)); 292 } else { 293 console.info('Operation succeed:' + JSON.stringify(data)); 294 } 295 }); 296} catch (err) { 297 console.error('Operation failed:' + JSON.stringify(err)); 298} 299``` 300## getBundlePackInfo 301 302getBundlePackInfo(bundleName: string, bundlePackFlag : BundlePackFlag): Promise\<BundlePackInfo>; 303 304基于bundleName和BundlePackFlag来获取bundlePackInfo。使用Promise异步回调。 305 306**系统接口:** 此接口为系统接口。 307 308**需要权限:** ohos.permission.GET_BUNDLE_INFO_PRIVILEGED 309 310**系统能力:** SystemCapability.BundleManager.BundleFramework.FreeInstall 311 312**参数:** 313 314| 参数名 | 类型 | 必填 | 说明 | 315| -------------- | --------------------------------- | ---- | ---------------------- | 316| bundleName | string | 是 | 应用程序Bundle名称。 | 317| bundlePackFlag | [BundlePackFlag](#bundlepackflag) | 是 | 指示要查询的应用包标志。| 318 319**返回值:** 320 321| 类型 | 说明 | 322| ---------------------------------------------------------- | ----------------------------------- | 323| Promise<[BundlePackInfo](js-apis-bundleManager-BundlePackInfo.md)> | Promise对象,返回BundlePackInfo信息。 | 324 325**错误码:** 326 327以下错误码的详细介绍请参见[ohos.bundle错误码](../errorcodes/errorcode-bundle.md)。 328 329| 错误码ID | 错误信息 | 330|----------|----------------------------------------| 331| 17700001 | The specified bundle name is not found. | 332 333**示例:** 334 335```js 336import freeInstall from '@ohos.bundle.freeInstall'; 337import { BusinessError } from '@ohos.base'; 338let bundleName = 'com.example.myapplication'; 339let bundlePackFlag = freeInstall.BundlePackFlag.GET_PACK_INFO_ALL; 340try { 341 freeInstall.getBundlePackInfo(bundleName, bundlePackFlag).then(data => { 342 console.info('Operation succeed:' + JSON.stringify(data)); 343 }).catch((err: BusinessError) => { 344 console.error('Operation failed:' + JSON.stringify(err)); 345 }); 346} catch (err) { 347 console.error('Operation failed:' + JSON.stringify(err)); 348} 349``` 350 351## getDispatchInfo 352 353getDispatchInfo(callback: AsyncCallback\<DispatchInfo>): void; 354 355获取有关dispatch版本的信息。使用callback异步回调。 356 357**系统接口:** 此接口为系统接口。 358 359**需要权限:** ohos.permission.GET_BUNDLE_INFO_PRIVILEGED 360 361**系统能力:** SystemCapability.BundleManager.BundleFramework.FreeInstall 362 363**参数:** 364 365| 参数名 | 类型 | 必填 | 说明 | 366| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 367| callback | AsyncCallback<[DispatchInfo](js-apis-bundleManager-dispatchInfo.md)> | 是 | 回调函数。当函数调用成功,err为null,data为获取到的[DispatchInfo](js-apis-bundleManager-dispatchInfo.md)信息。否则为错误对象。 | 368 369**示例:** 370 371```js 372import freeInstall from '@ohos.bundle.freeInstall'; 373try { 374 freeInstall.getDispatchInfo((err, data) => { 375 if (err) { 376 console.error('Operation failed:' + JSON.stringify(err)); 377 } else { 378 console.info('Operation succeed:' + JSON.stringify(data)); 379 } 380 }); 381} catch (err) { 382 console.error('Operation failed:' + JSON.stringify(err)); 383} 384``` 385 386## getDispatchInfo 387 388getDispatchInfo(): Promise\<DispatchInfo>; 389 390获取有关dispatch版本的信息。使用Promise异步回调。 391 392**系统接口:** 此接口为系统接口。 393 394**需要权限:** ohos.permission.GET_BUNDLE_INFO_PRIVILEGED 395 396**系统能力:** SystemCapability.BundleManager.BundleFramework.FreeInstall 397 398**返回值:** 399 400| 类型 | 说明 | 401| ------------------------------------------------ | ------------------------------------------------------------ | 402| Promise<[DispatchInfo](js-apis-bundleManager-dispatchInfo.md)> | Promise对象,返回[DispatchInfo](js-apis-bundleManager-dispatchInfo.md)信息。 | 403 404**示例:** 405 406```js 407import freeInstall from '@ohos.bundle.freeInstall'; 408import { BusinessError } from '@ohos.base'; 409try { 410 freeInstall.getDispatchInfo().then(data => { 411 console.info('Operation succeed:' + JSON.stringify(data)); 412 }).catch((err: BusinessError) => { 413 console.error('Operation failed:' + JSON.stringify(err)); 414 }); 415} catch (err) { 416 console.error('Operation failed:' + JSON.stringify(err)); 417} 418``` 419