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