js-apis-bundle-BundleInstaller.md 8.2 KB
Newer Older
1 2
# BundleInstaller

3
The **BundleInstaller** module provides APIs for you to install, uninstall, and recover bundles on devices.
4 5

> **NOTE**
6
>
7 8
> The initial APIs of this module are supported since API version 7. Newly added APIs will be marked with a superscript to indicate their earliest API version.

9 10 11
## BundleInstaller.install<sup>(deprecated)<sup>

> This API is deprecated since API version 9. You are advised to use [install](js-apis-installer.md) instead.
12 13 14

install(bundleFilePaths: Array&lt;string&gt;, param: InstallParam, callback: AsyncCallback&lt;InstallStatus&gt;): void;

15
Installs a bundle. Multiple HAP files can be installed. This API uses an asynchronous callback to return the result.
16 17 18 19 20 21 22 23 24

**Required permissions**

ohos.permission.INSTALL_BUNDLE

**System capability**

SystemCapability.BundleManager.BundleFramework

W
wusongqing 已提交
25 26
**System API**: This is a system API and cannot be called by third-party applications.

27 28
**Parameters**

29
| Name         | Type                                                | Mandatory| Description                                                        |
30
| --------------- | ---------------------------------------------------- | ---- | ------------------------------------------------------------ |
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
| bundleFilePaths | Array&lt;string&gt;                                  | Yes  | Sandbox path where the HAP files of the bundle are stored. For details about how to obtain the sandbox path, see [Obtaining the Sandbox Path](#obtaining-the-sandbox-path).|
| param           | [InstallParam](#installparamdeprecated)                        | Yes  | Parameters required for bundle installation.                                    |
| callback        | AsyncCallback&lt;[InstallStatus](#installstatusdeprecated)&gt; | Yes  | Callback used to return the installation status.              |

**Example**

```ts
import bundle from '@ohos.bundle';
let hapFilePaths = ['/data/storage/el2/base/haps/entry/files/'];
let installParam = {
    userId: 100,
    isKeepData: false,
    installFlag: 1,
};

bundle.getBundleInstaller().then(installer=>{
    installer.install(hapFilePaths, installParam, err => {
        if (err) {
            console.error('install failed:' + JSON.stringify(err));
        } else {
            console.info('install successfully.');
        }
    });
}).catch(error => {
    console.error('getBundleInstaller failed. Cause: ' + error.message);
});
```
58

59 60 61
## BundleInstaller.uninstall<sup>(deprecated)<sup>

> This API is deprecated since API version 9. You are advised to use [uninstall](js-apis-installer.md) instead.
62 63 64 65 66 67 68 69 70 71 72 73 74

uninstall(bundleName: string, param: InstallParam, callback: AsyncCallback&lt;InstallStatus&gt;): void;

Uninstalls a bundle. This API uses an asynchronous callback to return the result.

**Required permissions**

ohos.permission.INSTALL_BUNDLE

**System capability**

SystemCapability.BundleManager.BundleFramework

W
wusongqing 已提交
75 76
**System API**: This is a system API and cannot be called by third-party applications.

77 78
**Parameters**

79
| Name    | Type                                                | Mandatory| Description                                          |
80 81
| ---------- | ---------------------------------------------------- | ---- | ---------------------------------------------- |
| bundleName | string                                               | Yes  | Bundle name.                                          |
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
| param      | [InstallParam](#installparamdeprecated)                        | Yes  | Parameters required for bundle uninstall.                      |
| callback   | AsyncCallback&lt;[InstallStatus](#installstatusdeprecated)&gt; | Yes  | Callback used to return the installation status.|

**Example**

```ts
import bundle from '@ohos.bundle';
let bundleName = 'com.example.myapplication';
let installParam = {
    userId: 100,
    isKeepData: false,
    installFlag: 1,
};

bundle.getBundleInstaller().then(installer=>{
    installer.uninstall(bundleName, installParam, err => {
        if (err) {
            console.error('uninstall failed:' + JSON.stringify(err));
        } else {
            console.info('uninstall successfully.');
        }
    });
}).catch(error => {
    console.error('getBundleInstaller failed. Cause: ' + error.message);
});
```
108 109 110
## BundleInstaller.recover<sup>(deprecated)<sup>

> This API is deprecated since API version 9. You are advised to use [recover](js-apis-installer.md) instead.
111 112 113

recover(bundleName: string, param: InstallParam, callback: AsyncCallback&lt;InstallStatus&gt;): void;

114
Recovers a bundle. This API uses an asynchronous callback to return the result. After a pre-installed bundle is uninstalled, you can call this API to recover it.
115 116 117 118 119 120 121 122 123

**Required permissions**

ohos.permission.INSTALL_BUNDLE

**System capability**

SystemCapability.BundleManager.BundleFramework

W
wusongqing 已提交
124 125
**System API**: This is a system API and cannot be called by third-party applications.

126 127
**Parameters**

128
| Name    | Type                                                | Mandatory| Description                                          |
129 130
| ---------- | ---------------------------------------------------- | ---- | ---------------------------------------------- |
| bundleName | string                                               | Yes  | Bundle name.                                          |
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
| param      | [InstallParam](#installparamdeprecated)                        | Yes  | Parameters required for bundle recovery.                      |
| callback   | AsyncCallback&lt;[InstallStatus](#installstatusdeprecated)&gt; | Yes  | Callback used to return the recovery status.|

**Example**

```ts
import bundle from '@ohos.bundle';

let bundleName = 'com.example.myapplication';
let installParam = {
    userId: 100,
    isKeepData: false,
    installFlag: 1,
};

bundle.getBundleInstaller().then(installer=>{
    installer.recover(bundleName, installParam, err => {
        if (err) {
            console.error('recover failed:' + JSON.stringify(err));
        } else {
            console.info('recover successfully.');
        }
    });
}).catch(error => {
    console.error('getBundleInstaller failed. Cause: ' + error.message);
});
```
158

159
## InstallParam<sup>(deprecated)<sup>
160

161
Describes the parameters required for bundle installation, recovery, or uninstall.
162 163 164

 **System capability**: SystemCapability.BundleManager.BundleFramework

W
wusongqing 已提交
165 166
 **System API**: This is a system API and cannot be called by third-party applications.

167 168 169 170 171
| Name       | Type   | Readable| Writable| Description              |
| ----------- | ------- | ---- | ---- | ------------------ |
| userId      | number  | Yes  | No  | User ID.        |
| installFlag | number  | Yes  | No  | Installation flag.      |
| isKeepData  | boolean | Yes  | No  | Whether data is kept.|
172

173
## InstallStatus<sup>(deprecated)<sup>
174

175
Describes the bundle installation or uninstall status.
176 177 178

 **System capability**: SystemCapability.BundleManager.BundleFramework

W
wusongqing 已提交
179 180
 **System API**: This is a system API and cannot be called by third-party applications.

181 182 183 184
| Name         | Type                                                        | Readable| Writable| Description                          |
| ------------- | ------------------------------------------------------------ | ---- | ---- | ------------------------------ |
| status        | bundle.[InstallErrorCode](js-apis-Bundle.md#installerrorcode) | Yes  | No  | Installation or uninstall error code.      |
| statusMessage | string                                                       | Yes  | No  | Installation or uninstall status message.|
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208

## Obtaining the Sandbox Path
For the FA model, the sandbox path of a bundle can be obtained using the APIs in [Context](js-apis-inner-app-context.md). For the sage model, the sandbox path can be obtained using the attribute in [Context](js-apis-ability-context.md#abilitycontext). The following describes how to obtain the sandbox path.

**Example**
``` ts
// Stage model
import Ability from '@ohos.application.Ability';
class MainAbility extends Ability {
    onWindowStageCreate(windowStage) {
        let context = this.context;
        let pathDir = context.filesDir;
        console.info('sandbox path is ' + pathDir);
    }
}

// FA model
import featureAbility from '@ohos.ability.featureAbility';
let context = featureAbility.getContext();
context.getFilesDir().then((data) => {
    let pathDir = data;
    console.info('sandbox path is ' + pathDir);
});
```