1e41f4b71Sopenharmony_ci# BundleInstaller (系统接口) 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ci本模块提供设备上安装、升级和卸载应用的能力。 4e41f4b71Sopenharmony_ci 5e41f4b71Sopenharmony_ci> **说明:** 6e41f4b71Sopenharmony_ci> 7e41f4b71Sopenharmony_ci> 本模块首批接口从API version 7 开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 8e41f4b71Sopenharmony_ci> 9e41f4b71Sopenharmony_ci> 本模块为系统接口。 10e41f4b71Sopenharmony_ci 11e41f4b71Sopenharmony_ci## BundleInstaller.install<sup>(deprecated)<sup> 12e41f4b71Sopenharmony_ci 13e41f4b71Sopenharmony_ci> 从API version 9开始不再维护,建议使用[@ohos.bundle.installer.install](js-apis-installer-sys.md)替代。 14e41f4b71Sopenharmony_ci 15e41f4b71Sopenharmony_ciinstall(bundleFilePaths: Array<string>, param: InstallParam, callback: AsyncCallback<InstallStatus>): void; 16e41f4b71Sopenharmony_ci 17e41f4b71Sopenharmony_ci以异步方法在应用中安装hap,支持多hap安装。使用callback形式返回结果。 18e41f4b71Sopenharmony_ci 19e41f4b71Sopenharmony_ci**需要权限:** 20e41f4b71Sopenharmony_ci 21e41f4b71Sopenharmony_ciohos.permission.INSTALL_BUNDLE 22e41f4b71Sopenharmony_ci 23e41f4b71Sopenharmony_ci**系统能力:** 24e41f4b71Sopenharmony_ci 25e41f4b71Sopenharmony_ciSystemCapability.BundleManager.BundleFramework 26e41f4b71Sopenharmony_ci 27e41f4b71Sopenharmony_ci**系统接口:** 此接口为系统接口。 28e41f4b71Sopenharmony_ci 29e41f4b71Sopenharmony_ci**参数:** 30e41f4b71Sopenharmony_ci 31e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 32e41f4b71Sopenharmony_ci| --------------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 33e41f4b71Sopenharmony_ci| bundleFilePaths | Array<string> | 是 | 指示存储HAP的沙箱路径。沙箱路径的获取方法参见[获取应用的沙箱路径](#获取应用的沙箱路径)。 | 34e41f4b71Sopenharmony_ci| param | [InstallParam](#installparamdeprecated) | 是 | 指定安装所需的其他参数。 | 35e41f4b71Sopenharmony_ci| callback | AsyncCallback<[InstallStatus](#installstatusdeprecated)> | 是 | 程序启动作为入参的回调函数,返回安装状态信息。 | 36e41f4b71Sopenharmony_ci 37e41f4b71Sopenharmony_ci**示例:** 38e41f4b71Sopenharmony_ci 39e41f4b71Sopenharmony_ci```ts 40e41f4b71Sopenharmony_ciimport bundleInstall from '@ohos.bundle.installer'; 41e41f4b71Sopenharmony_ciimport { BusinessError } from '@ohos.base'; 42e41f4b71Sopenharmony_ci 43e41f4b71Sopenharmony_cilet hapFilePaths: Array<string> = ['/data/storage/el2/base/haps/entry/files/']; 44e41f4b71Sopenharmony_cilet installParam: bundleInstall.InstallParam = { 45e41f4b71Sopenharmony_ci userId: 100, 46e41f4b71Sopenharmony_ci isKeepData: false, 47e41f4b71Sopenharmony_ci installFlag: 1, 48e41f4b71Sopenharmony_ci}; 49e41f4b71Sopenharmony_ci 50e41f4b71Sopenharmony_cibundleInstall.getBundleInstaller().then(installer => { 51e41f4b71Sopenharmony_ci installer.install(hapFilePaths, installParam, err => { 52e41f4b71Sopenharmony_ci if (err) { 53e41f4b71Sopenharmony_ci console.error('install failed:' + JSON.stringify(err)); 54e41f4b71Sopenharmony_ci } else { 55e41f4b71Sopenharmony_ci console.info('install successfully.'); 56e41f4b71Sopenharmony_ci } 57e41f4b71Sopenharmony_ci }); 58e41f4b71Sopenharmony_ci}).catch((error: BusinessError)=> { 59e41f4b71Sopenharmony_ci let message = (error as BusinessError).message; 60e41f4b71Sopenharmony_ci console.error('getBundleInstaller failed. Cause: ' + message); 61e41f4b71Sopenharmony_ci}); 62e41f4b71Sopenharmony_ci``` 63e41f4b71Sopenharmony_ci 64e41f4b71Sopenharmony_ci## BundleInstaller.uninstall<sup>(deprecated)<sup> 65e41f4b71Sopenharmony_ci 66e41f4b71Sopenharmony_ci> 从API version 9开始不再维护,建议使用[uninstall](js-apis-installer-sys.md)替代。 67e41f4b71Sopenharmony_ci 68e41f4b71Sopenharmony_ciuninstall(bundleName: string, param: InstallParam, callback: AsyncCallback<InstallStatus>): void; 69e41f4b71Sopenharmony_ci 70e41f4b71Sopenharmony_ci以异步方法卸载应用程序,使用callback异步回调,返回安装状态信息。 71e41f4b71Sopenharmony_ci 72e41f4b71Sopenharmony_ci**需要权限:** 73e41f4b71Sopenharmony_ci 74e41f4b71Sopenharmony_ciohos.permission.INSTALL_BUNDLE 75e41f4b71Sopenharmony_ci 76e41f4b71Sopenharmony_ci**系统能力:** 77e41f4b71Sopenharmony_ci 78e41f4b71Sopenharmony_ciSystemCapability.BundleManager.BundleFramework 79e41f4b71Sopenharmony_ci 80e41f4b71Sopenharmony_ci**系统接口:** 此接口为系统接口。 81e41f4b71Sopenharmony_ci 82e41f4b71Sopenharmony_ci**参数:** 83e41f4b71Sopenharmony_ci 84e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 85e41f4b71Sopenharmony_ci| ---------- | ------------------------------------------------------------ | ---- | ---------------------------------------------- | 86e41f4b71Sopenharmony_ci| bundleName | string | 是 | 应用Bundle名称。 | 87e41f4b71Sopenharmony_ci| param | [InstallParam](#installparamdeprecated) | 是 | 指定卸载所需的其他参数。 | 88e41f4b71Sopenharmony_ci| callback | AsyncCallback<[InstallStatus](#installstatusdeprecated)> | 是 | 程序启动作为入参的回调函数,返回安装状态信息。 | 89e41f4b71Sopenharmony_ci 90e41f4b71Sopenharmony_ci**示例:** 91e41f4b71Sopenharmony_ci 92e41f4b71Sopenharmony_ci```ts 93e41f4b71Sopenharmony_ciimport bundleInstall from '@ohos.bundle.installer'; 94e41f4b71Sopenharmony_ciimport { BusinessError } from '@ohos.base'; 95e41f4b71Sopenharmony_ci 96e41f4b71Sopenharmony_cilet bundleName: string = 'com.example.myapplication'; 97e41f4b71Sopenharmony_cilet installParam: bundleInstall.InstallParam = { 98e41f4b71Sopenharmony_ci userId: 100, 99e41f4b71Sopenharmony_ci isKeepData: false, 100e41f4b71Sopenharmony_ci installFlag: 1, 101e41f4b71Sopenharmony_ci}; 102e41f4b71Sopenharmony_ci 103e41f4b71Sopenharmony_cibundleInstall.getBundleInstaller().then(installer => { 104e41f4b71Sopenharmony_ci installer.uninstall(bundleName, installParam, err => { 105e41f4b71Sopenharmony_ci if (err) { 106e41f4b71Sopenharmony_ci console.error('uninstall failed:' + JSON.stringify(err)); 107e41f4b71Sopenharmony_ci } else { 108e41f4b71Sopenharmony_ci console.info('uninstall successfully.'); 109e41f4b71Sopenharmony_ci } 110e41f4b71Sopenharmony_ci }); 111e41f4b71Sopenharmony_ci}).catch((error: BusinessError) => { 112e41f4b71Sopenharmony_ci let message = (error as BusinessError).message; 113e41f4b71Sopenharmony_ci console.error('getBundleInstaller failed. Cause: ' + message); 114e41f4b71Sopenharmony_ci}); 115e41f4b71Sopenharmony_ci``` 116e41f4b71Sopenharmony_ci## BundleInstaller.recover<sup>(deprecated)<sup> 117e41f4b71Sopenharmony_ci 118e41f4b71Sopenharmony_ci> 从API version 9开始不再维护,建议使用[recover](js-apis-installer-sys.md)替代。 119e41f4b71Sopenharmony_ci 120e41f4b71Sopenharmony_cirecover(bundleName: string, param: InstallParam, callback: AsyncCallback<InstallStatus>): void; 121e41f4b71Sopenharmony_ci 122e41f4b71Sopenharmony_ci以异步方法恢复一个应用程序,使用callback形式返回结果。当预置应用被卸载后,可以通过此接口进行恢复。 123e41f4b71Sopenharmony_ci 124e41f4b71Sopenharmony_ci**需要权限:** 125e41f4b71Sopenharmony_ci 126e41f4b71Sopenharmony_ciohos.permission.INSTALL_BUNDLE 127e41f4b71Sopenharmony_ci 128e41f4b71Sopenharmony_ci**系统能力:** 129e41f4b71Sopenharmony_ci 130e41f4b71Sopenharmony_ciSystemCapability.BundleManager.BundleFramework 131e41f4b71Sopenharmony_ci 132e41f4b71Sopenharmony_ci**系统接口:** 此接口为系统接口。 133e41f4b71Sopenharmony_ci 134e41f4b71Sopenharmony_ci**参数:** 135e41f4b71Sopenharmony_ci 136e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 137e41f4b71Sopenharmony_ci| ---------- | ------------------------------------------------------------ | ---- | -------------------------------------------------- | 138e41f4b71Sopenharmony_ci| bundleName | string | 是 | 应用Bundle名称。 | 139e41f4b71Sopenharmony_ci| param | [InstallParam](#installparamdeprecated) | 是 | 指定应用恢复所需的其他参数。 | 140e41f4b71Sopenharmony_ci| callback | AsyncCallback<[InstallStatus](#installstatusdeprecated)> | 是 | 程序启动作为入参的回调函数,返回应用恢复状态信息。 | 141e41f4b71Sopenharmony_ci 142e41f4b71Sopenharmony_ci**示例:** 143e41f4b71Sopenharmony_ci 144e41f4b71Sopenharmony_ci```ts 145e41f4b71Sopenharmony_ciimport bundleInstall from '@ohos.bundle.installer'; 146e41f4b71Sopenharmony_ciimport { BusinessError } from '@ohos.base'; 147e41f4b71Sopenharmony_ci 148e41f4b71Sopenharmony_cilet bundleName: string = 'com.example.myapplication'; 149e41f4b71Sopenharmony_cilet installParam: bundleInstall.InstallParam = { 150e41f4b71Sopenharmony_ci userId: 100, 151e41f4b71Sopenharmony_ci isKeepData: false, 152e41f4b71Sopenharmony_ci installFlag: 1, 153e41f4b71Sopenharmony_ci}; 154e41f4b71Sopenharmony_ci 155e41f4b71Sopenharmony_cibundleInstall.getBundleInstaller().then(installer => { 156e41f4b71Sopenharmony_ci installer.uninstall(bundleName, installParam, err => { 157e41f4b71Sopenharmony_ci if (err) { 158e41f4b71Sopenharmony_ci console.error('uninstall failed:' + JSON.stringify(err)); 159e41f4b71Sopenharmony_ci } else { 160e41f4b71Sopenharmony_ci console.info('uninstall successfully.'); 161e41f4b71Sopenharmony_ci } 162e41f4b71Sopenharmony_ci }); 163e41f4b71Sopenharmony_ci}).catch((error: BusinessError) => { 164e41f4b71Sopenharmony_ci let message = (error as BusinessError).message; 165e41f4b71Sopenharmony_ci console.error('getBundleInstaller failed. Cause: ' + message); 166e41f4b71Sopenharmony_ci}); 167e41f4b71Sopenharmony_ci``` 168e41f4b71Sopenharmony_ci 169e41f4b71Sopenharmony_ci## InstallParam<sup>(deprecated)<sup> 170e41f4b71Sopenharmony_ci 171e41f4b71Sopenharmony_ci安装、恢复或卸载时需要指定的参数。 172e41f4b71Sopenharmony_ci 173e41f4b71Sopenharmony_ci **系统能力:** 以下各项对应的系统能力均为SystemCapability.BundleManager.BundleFramework 174e41f4b71Sopenharmony_ci 175e41f4b71Sopenharmony_ci **系统接口:** 此接口为系统接口。 176e41f4b71Sopenharmony_ci 177e41f4b71Sopenharmony_ci| 名称 | 类型 | 只读 | 可选 | 说明 | 178e41f4b71Sopenharmony_ci| ----------- | ------- | ---- | ---- | ------------------ | 179e41f4b71Sopenharmony_ci| userId | number | 否 | 否 | 指示用户id, 默认值:调用方的userId | 180e41f4b71Sopenharmony_ci| installFlag | number | 否 | 否 | 指示安装标志, 默认值:1, 取值范围:</br>1: 覆盖安装, </br>16: 免安装| 181e41f4b71Sopenharmony_ci| isKeepData | boolean | 否 | 否 | 指示参数是否有数据,默认值:false | 182e41f4b71Sopenharmony_ci 183e41f4b71Sopenharmony_ci## InstallStatus<sup>(deprecated)<sup> 184e41f4b71Sopenharmony_ci 185e41f4b71Sopenharmony_ci应用程序安装卸载的结果。 186e41f4b71Sopenharmony_ci 187e41f4b71Sopenharmony_ci **系统能力:** 以下各项对应的系统能力均为SystemCapability.BundleManager.BundleFramework 188e41f4b71Sopenharmony_ci 189e41f4b71Sopenharmony_ci **系统接口:** 此接口为系统接口。 190e41f4b71Sopenharmony_ci 191e41f4b71Sopenharmony_ci| 名称 | 类型 | 只读 | 可选 | 说明 | 192e41f4b71Sopenharmony_ci| ------------- | ------------------------------------------------------------ | ---- | ---- | ------------------------------------------------------------ | 193e41f4b71Sopenharmony_ci| status | bundle.[InstallErrorCode](js-apis-Bundle.md#installerrorcode) | 否 | 否 | 表示安装或卸载错误状态码。取值范围:枚举值[InstallErrorCode](js-apis-Bundle.md#installerrorcode) | 194e41f4b71Sopenharmony_ci| statusMessage | string | 否 | 否 | 表示安装或卸载的字符串结果信息。取值范围包括:<br/> "SUCCESS" : 安装成功,</br> "STATUS_INSTALL_FAILURE": 安装失败(不存在安装文件), </br> "STATUS_INSTALL_FAILURE_ABORTED": 安装中止, </br> "STATUS_INSTALL_FAILURE_INVALID": 安装参数无效, </br> "STATUS_INSTALL_FAILURE_CONFLICT": 安装冲突(常见于升级和已有应用基本信息不一致), </br> "STATUS_INSTALL_FAILURE_STORAGE": 存储包信息失败, </br> "STATUS_INSTALL_FAILURE_INCOMPATIBLE": 安装不兼容(常见于版本降级安装或者签名信息错误), </br> "STATUS_UNINSTALL_FAILURE": 卸载失败(不存在卸载的应用), </br> "STATUS_UNINSTALL_FAILURE_ABORTED": 卸载中止(没有使用), </br> "STATUS_UNINSTALL_FAILURE_ABORTED": 卸载冲突(卸载系统应用失败, 结束应用进程失败), </br> "STATUS_INSTALL_FAILURE_DOWNLOAD_TIMEOUT": 安装失败(下载超时), </br> "STATUS_INSTALL_FAILURE_DOWNLOAD_FAILED": 安装失败(下载失败), </br> "STATUS_RECOVER_FAILURE_INVALID": 恢复预置应用失败, </br> "STATUS_ABILITY_NOT_FOUND": Ability未找到, </br> "STATUS_BMS_SERVICE_ERROR": BMS服务错误, </br> "STATUS_FAILED_NO_SPACE_LEFT": 设备空间不足, </br> "STATUS_GRANT_REQUEST_PERMISSIONS_FAILED": 应用授权失败, </br> "STATUS_INSTALL_PERMISSION_DENIED": 缺少安装权限, </br> "STATUS_UNINSTALL_PERMISSION_DENIED": 缺少卸载权限 | 195e41f4b71Sopenharmony_ci 196e41f4b71Sopenharmony_ci## 获取应用的沙箱路径 197e41f4b71Sopenharmony_ci对于FA模型,应用的沙箱路径可以通过[Context](js-apis-inner-app-context.md)中的方法获取;对于Stage模型,应用的沙箱路径可以通过[Context](js-apis-inner-application-uiAbilityContext-sys.md#abilitycontext)中的属性获取。下面以获取沙箱文件路径为例。 198e41f4b71Sopenharmony_ci 199e41f4b71Sopenharmony_ci**示例:** 200e41f4b71Sopenharmony_ci``` ts 201e41f4b71Sopenharmony_ci// Stage模型 202e41f4b71Sopenharmony_ciimport UIAbility from '@ohos.app.ability.UIAbility'; 203e41f4b71Sopenharmony_ciimport window from '@ohos.window'; 204e41f4b71Sopenharmony_ciexport default class EntryAbility extends UIAbility { 205e41f4b71Sopenharmony_ci onWindowStageCreate(windowStage: window.WindowStage) { 206e41f4b71Sopenharmony_ci let context = this.context; 207e41f4b71Sopenharmony_ci let pathDir = context.filesDir; 208e41f4b71Sopenharmony_ci console.info('sandbox path is ' + pathDir); 209e41f4b71Sopenharmony_ci } 210e41f4b71Sopenharmony_ci} 211e41f4b71Sopenharmony_ci``` 212e41f4b71Sopenharmony_ci 213e41f4b71Sopenharmony_ci<!--code_no_check_fa--> 214e41f4b71Sopenharmony_ci``` ts 215e41f4b71Sopenharmony_ci// FA模型 216e41f4b71Sopenharmony_ciimport featureAbility from '@ohos.ability.featureAbility'; 217e41f4b71Sopenharmony_cilet context = featureAbility.getContext(); 218e41f4b71Sopenharmony_cicontext.getFilesDir().then((data: string) => { 219e41f4b71Sopenharmony_ci let pathDir = data; 220e41f4b71Sopenharmony_ci console.info('sandbox path is ' + pathDir); 221e41f4b71Sopenharmony_ci}); 222e41f4b71Sopenharmony_ci```