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