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