js-apis-installer.md 12.0 KB
Newer Older
W
wanghang 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 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 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 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 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 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 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
# BundleInstaller

> ![icon-note.gif](public_sys-resources/icon-note.gif) **说明:**
> 本模块首批接口从API version 9 开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。

在设备上安装、升级和卸载应用

## 导入模块

```js
import installer from '@ohos.bundle.installer';
```

## 权限列表

| 权限                                       | 权限等级     | 描述               |
| ------------------------------------------ | ------------ | ------------------ |
| ohos.permission.INSTALL_BUNDLE             | system_core  | 可安装、卸载应用   |

权限等级参考[权限等级说明](https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/security/accesstoken-overview.md#%E6%9D%83%E9%99%90%E7%AD%89%E7%BA%A7%E8%AF%B4%E6%98%8E)

## BundleInstaller.getBundleInstaller

getBundleInstaller(callback: AsyncCallback\<BundleInstaller>): void;

获取BundleInstaller对象,使用callback形式返回结果。

**系统接口:** 此接口为系统接口,三方应用不支持调用

**系统能力:** SystemCapability.BundleManager.BundleFramework.Core

**参数:**

| 名称     | 类型                                                         | 必填 | 描述                                                         |
| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
| callback | AsyncCallback\<[BundleInstaller](js-apis-installer.md#BundleInstaller)> | 是   | 回调函数,获取BundleInstaller对象,err为undefined,data为获取到的BundleInstaller对象;否则为错误对象 |

**错误码:**

错误码的详细介绍请参见[ohos.bundle错误码](../errorcodes/errcode-bundle.md)

**示例:**

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

try {
    installer.getBundleInstaller((err, data) => {
        if (err) {
            console.error('getBundleInstaller failed:' + err.message);
        } else {
            console.info('getBundleInstaller successfully');
        }
    });
} catch (error) {
    console.error('getBundleInstaller failed:' + error.message);
}
```

## BundleInstaller.getBundleInstaller

getBundleInstaller(): Promise\<BundleInstaller>;

获取BundleInstaller对象,使用callback形式返回结果。

**系统接口:** 此接口为系统接口,三方应用不支持调用

**系统能力:** SystemCapability.BundleManager.BundleFramework.Core

**返回值:**
| 类型                                                         | 说明                                 |
| ------------------------------------------------------------ | ------------------------------------ |
| Promise\<[BundleInstaller](js-apis-installer.md#BundleInstaller)> | Promise对象,返回BundleInstaller对象 |

**错误码:**

错误码的详细介绍请参见[ohos.bundle错误码](../errorcodes/errcode-bundle.md)

**示例:**

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

try {
    installer.getBundleInstaller().then((data) => {
        console.info('getBundleInstaller successfully.');
    }).catch((error) => {
        console.error('getBundleInstaller failed. Cause: ' + error.message);
    });
} catch (error) {
    console.error('getBundleInstaller failed. Cause: ' + error.message);
}
```

## BundleInstaller.install
install(hapFilePaths: Array&lt;string&gt;, installParam: InstallParam, callback: AsyncCallback&lt;void&gt;): void;

以异步方法安装应用,使用callback形式返回结果。

**系统接口:** 此接口为系统接口,三方应用不支持调用

**需要权限:** ohos.permission.INSTALL_BUNDLE

**系统能力:** SystemCapability.BundleManager.BundleFramework.Core

**参数:**

| 名称            | 类型                                                 | 必填 | 描述                                                         |
| --------------- | ---------------------------------------------------- | ---- | ------------------------------------------------------------ |
| hapFilePaths | Array&lt;string&gt;                                  | 是   | 存储应用程序包的路径。路径应该是当前应用程序中存放HAP包的数据目录。当传入的路径是一个目录时, 该目录下只能放同一个应用的HAP包,且这些HAP包的签名需要保持一致 |
| installParam           | [InstallParam](#installparam)                        | 是   | 指定安装所需的其他参数                                     |
| callback | AsyncCallback&lt;void&gt; | 是 | 回调函数,安装应用成功,err为undefined,否则为错误对象 |

**错误码:**

以下错误码的详细介绍请参见[ohos.bundle错误码](../errorcodes/errcode-bundle.md)

| 错误码ID | 错误码信息                                                      |
| -------- | ---------------------------------------------------------------|
| 17700004 | The specified userId is not existed                            |
| 17700010 | To parse file of config.json or module.json failed             |
| 17700011 | To verify signature failed                                     |
| 17700012 | Invalid hap file path or too large file size                   |
| 17700015 | Multiple haps have inconsistent configured information         |
| 17700016 | No disk space left for installation                            |
| 17700017 | Downgrade installation is prohibited                           |
| 17700101 | The system service is excepted                                 |
| 17700103 | I/O operation is failed                                        |

**示例:**

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

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

## BundleInstaller.uninstall

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

以异步方法卸载应用,使用callback形式返回结果。

**系统接口:** 此接口为系统接口,三方应用不支持调用

**需要权限:** ohos.permission.INSTALL_BUNDLE

**系统能力:** SystemCapability.BundleManager.BundleFramework.Core

**参数:**

| 名称       | 类型                                                 | 必填 | 描述                                           |
| ---------- | ---------------------------------------------------- | ---- | ---------------------------------------------- |
| bundleName | string                                               | 是   | 包名                                           |
| installParam      | [InstallParam](#installparam)                        | 是   | 指定安装所需的其他参数                       |
| callback | AsyncCallback&lt;void&gt; | 是 | 回调函数,卸载应用成功,err为undefined,否则为错误对象 |

**错误码:**

以下错误码的详细介绍请参见[ohos.bundle错误码](../errorcodes/errcode-bundle.md)

| 错误码ID | 错误码信息                                                                  |
| -------- | ---------------------------------------------------------------------------|
| 17700004 | The specified userId is not existed                                        |
| 17700020 | The specified bundle is pre-installed bundle which cannot be uninstalled   |
| 17700101 | The system service is excepted                                             |

**示例:**

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

try {
    installer.getBundleInstaller().then(data => {
        data.uninstall(bundleName, installParam, err => {
            if (err) {
                console.error('uninstall failed:' + err.message);
            } else {
                console.info('uninstall successfully.');
            }
        });
    }).catch(error => {
        console.error('getBundleInstaller failed. Cause: ' + error.message);
    });
} catch (error) {
    console.error('getBundleInstaller failed. Cause: ' + error.message);
}
```

## BundleInstaller.recover

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

以异步方法回滚应用,使用callback形式返回结果。

**系统接口:** 此接口为系统接口,三方应用不支持调用

**需要权限:** ohos.permission.INSTALL_BUNDLE

**系统能力:** SystemCapability.BundleManager.BundleFramework.Core

**参数:**

| 名称       | 类型                                                 | 必填 | 描述                                           |
| ---------- | ---------------------------------------------------- | ---- | ---------------------------------------------- |
| bundleName | string                                               | 是   | 包名                                           |
| installParam      | [InstallParam](#installparam)                        | 是   | 指定安装所需的其他参数                       |
| callback | AsyncCallback&lt;void&gt; | 是 | 回调函数,回滚应用成功,err为undefined,否则为错误对象 |

**错误码:**

以下错误码的详细介绍请参见[ohos.bundle错误码](../errorcodes/errcode-bundle.md)

| 错误码ID | 错误码信息                                                                 |
| -------- | ---------------------------------------------------------------------------|
| 17700004 | The specified userId is not existed                                        |

**示例:**

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

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

## HashParam

应用程序安装卸载信息

 **系统能力:** SystemCapability.BundleManager.BundleFramework.Core

 **系统接口:** 此接口为系统接口,三方应用不支持调用

| 名称       | 类型   | 说明             |
| ---------- | ------ | ---------------- |
| moduleName | string | 应用程序模块名称 |
| hashValue  | string | 哈希值           |

## InstallParam

应用程序安装卸载信息

 **系统能力:** SystemCapability.BundleManager.BundleFramework.Core

 **系统接口:** 此接口为系统接口,三方应用不支持调用

| 名称                           | 类型                           | 说明               |
| ------------------------------ | ------------------------------ | ------------------ |
| userId                         | number                         | 指示用户id,可使用[queryOsAccountLocalIdFromProcess](js-apis-osAccount.md#queryosaccountlocalidfromprocess9)获取当前进程所在用户 |
| installFlag                    | number                         | 指示安装标志,枚举值:0:应用初次安装,1:应用覆盖安装 |
| isKeepData                     | boolean                        | 卸载时是否保留数据目录 |
| hashParams        | Array<[HashParam](#hashparam)> | 哈希值参数         |
| crowdtestDeadline| number                         | 测试包的被杀死时间 |