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