js-apis-bundle-appControl.md 13.8 KB
Newer Older
X
xsz233 已提交
1
# appControl模块
X
xsz233 已提交
2

X
xsz233 已提交
3
本模块提供应用拦截能力。对应用设置处置状态后,应用会被禁止运行;用户点击桌面图标时,会根据应用的处置状态,跳转到对应的页面。本模块支持对应用的处置状态进行设置、获取、删除。
X
xsz233 已提交
4 5 6 7 8

> **说明:**
>
> 本模块首批接口从API version 9开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。

X
xsz233 已提交
9 10
本模块接口为系统接口。

X
xsz233 已提交
11 12
## 导入模块

X
xsz233 已提交
13
``` ts
X
xsz233 已提交
14 15 16 17 18
import appControl from '@ohos.bundle.appControl'
```

## appControl.setDisposedStatus

X
xsz233 已提交
19
setDisposedStatus(appId: string, disposedWant: Want): Promise\<void>
X
xsz233 已提交
20

X
xsz233 已提交
21
以异步方法设置应用的处置状态。使用Promise异步回调。成功返回null,失败返回对应错误信息。
X
xsz233 已提交
22 23 24 25 26

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

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

X
xsz233 已提交
27
**系统API:**  此接口为系统接口,三方应用不支持调用。
X
xsz233 已提交
28 29 30 31 32

**参数:**

| 名称          | 类型     | 必填   | 描述                                      |
| ----------- | ------ | ---- | --------------------------------------- |
X
xsz233 已提交
33
| appId  | string | 是    | 需要设置处置状态的应用的appId。<br> appId是应用的唯一标识,由应用的包名和签名信息决定,可以通过getBundleInfo接口获取。                    |
X
xsz233 已提交
34
| disposedWant | Want  | 是 | 对应用的处置意图。 |
X
xsz233 已提交
35 36 37 38 39

**返回值:**

| 类型                        | 说明                 |
| ------------------------- | ------------------ |
X
xsz233 已提交
40
| Promise\<void> | Promise对象。无返回结果的Promise对象。 |
X
xsz233 已提交
41

X
xsz233 已提交
42
**错误码**
X
xsz233 已提交
43

X
xsz233 已提交
44 45
以下错误码的详细介绍请参见[ohos.bundle错误码](../errorcodes/errcode-bundle.md)

X
xsz233 已提交
46 47 48 49 50 51
| 错误码 | 错误信息                                |
| ------ | -------------------------------------- |
| 17700005 |  The specified appId was not found.  |

**示例:**

X
xsz233 已提交
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
```ts
import appControl from '@ohos.bundle.appControl'
import bundleManager from '@ohos.bundle.bundleManager';

// 获取appId
var bundleName = 'com.example.myapplication';
var appId;
try {
    bundleManager.getBundleInfo(bundleName, BundleManager.BundleFlags.GET_BUNDLE_INFO_WITH_SIGNATURE_INFO)
        .then((data) => {
            appId = data.signatureInfo.appId;
        }, error => {
            console.error("getBundleInfo failed " + error.message);
        });
} catch (error) {
    console.error("getBundleInfo failed " + error.message);
}

X
xsz233 已提交
70
var want = {bundleName: 'com.example.myapplication'};
X
xsz233 已提交
71 72 73

try {
    appControl.setDisposedStatus(appId, want)
X
xsz233 已提交
74 75 76 77 78
        .then(() => {
            console.info('setDisposedStatus success');
        }).catch((error) => {
            console.error('setDisposedStatus failed ' + error.message);
        });
X
xsz233 已提交
79
} catch (error) {
X
xsz233 已提交
80
    console.error('setDisposedStatus failed ' + error.message);
X
xsz233 已提交
81 82 83 84 85
}
```

## appControl.setDisposedStatus

X
xsz233 已提交
86
setDisposedStatus(appId: string, disposedWant: Want, callback: AsyncCallback\<void>): void;
X
xsz233 已提交
87

X
xsz233 已提交
88
以异步方法设置应用的处置状态。使用callback异步回调。成功返回null,失败返回对应错误信息。
X
xsz233 已提交
89 90 91 92 93 94 95 96 97 98 99

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

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

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

**参数:**

| 名称          | 类型                              | 必填   | 描述                                      |
| ----------- | ------------------------------- | ---- | --------------------------------------- |
X
xsz233 已提交
100
| appId  | string | 是    | 需要设置处置的应用的appId<br> appId是应用的唯一标识,由应用的包名和签名信息决定,可以通过getBundleInfo接口获取。                           |
X
xsz233 已提交
101 102
| disposedWant | Want  | 是 | 对应用的处置意图。 |
| callback    | AsyncCallback\<void> | 是    | 回调函数,当设置处置状态成功,err为undefined,否则为错误对象。 |
X
xsz233 已提交
103

X
xsz233 已提交
104
**错误码**
X
xsz233 已提交
105

X
xsz233 已提交
106 107
以下错误码的详细介绍请参见[ohos.bundle错误码](../errorcodes/errcode-bundle.md)

X
xsz233 已提交
108 109 110 111 112 113
| 错误码 | 错误信息                                |
| ------ | -------------------------------------- |
| 17700005 |  The specified appId was not found.  |

**示例:**

X
xsz233 已提交
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
```ts
import appControl from '@ohos.bundle.appControl'
import bundleManager from '@ohos.bundle.bundleManager';

// 获取appId
var bundleName = 'com.example.myapplication';
var appId;
try {
    bundleManager.getBundleInfo(bundleName, BundleManager.BundleFlags.GET_BUNDLE_INFO_WITH_SIGNATURE_INFO)
        .then((data) => {
            appId = data.signatureInfo.appId;
        }, error => {
            console.error("getBundleInfo failed " + error.message);
        });
} catch (error) {
    console.error("getBundleInfo failed " + error.message);
}

X
xsz233 已提交
132
var want = {bundleName: 'com.example.myapplication'};
X
xsz233 已提交
133 134 135 136

try {
    appControl.setDisposedStatus(appId, want, (err, data) => {
        if (err) {
X
xsz233 已提交
137
            console.error('setDisposedStatus failed ' + error.message);
X
xsz233 已提交
138 139
            return;
        }
X
xsz233 已提交
140
         console.info('setDisposedStatus success');
X
xsz233 已提交
141 142
    });
} catch (error) {
X
xsz233 已提交
143
    console.error('setDisposedStatus failed ' + error.message);
X
xsz233 已提交
144 145 146 147 148
}
```

## appControl.getDisposedStatus

X
xsz233 已提交
149
getDisposedStatus(appId: string): Promise\<Want>;
X
xsz233 已提交
150

X
xsz233 已提交
151
以异步方法获取指定应用已设置的处置状态。使用Promise异步回调,成功返回应用的处置状态,失败返回对应错误信息。
X
xsz233 已提交
152 153 154 155 156 157 158 159 160 161 162

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

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

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

**参数:**

| 名称          | 类型     | 必填   | 描述                                      |
| ----------- | ------ | ---- | --------------------------------------- |
X
xsz233 已提交
163
| appId  | string | 是    | 要查询的应用的appId<br> appId是应用的唯一标识,由应用的包名和签名信息决定,可以通过getBundleInfo接口获取。       |
X
xsz233 已提交
164 165 166 167 168

**返回值:**

| 类型                        | 说明                 |
| ------------------------- | ------------------ |
X
xsz233 已提交
169
| Promise\<Want> | Promise对象,返回应用的处置状态。 |
X
xsz233 已提交
170

X
xsz233 已提交
171
**错误码**
X
xsz233 已提交
172

X
xsz233 已提交
173 174
以下错误码的详细介绍请参见[ohos.bundle错误码](../errorcodes/errcode-bundle.md)

X
xsz233 已提交
175 176 177 178 179 180
| 错误码 | 错误信息                                |
| ------ | -------------------------------------- |
| 17700005 |  The specified appId was not found.  |

**示例:**

X
xsz233 已提交
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
```ts
import appControl from '@ohos.bundle.appControl'
import bundleManager from '@ohos.bundle.bundleManager';

// 获取appId
var bundleName = 'com.example.myapplication';
var appId;
try {
    bundleManager.getBundleInfo(bundleName, BundleManager.BundleFlags.GET_BUNDLE_INFO_WITH_SIGNATURE_INFO)
        .then((data) => {
            appId = data.signatureInfo.appId;
        }, error => {
            console.error("getBundleInfo failed " + error.message);
        });
} catch (error) {
    console.error("getBundleInfo failed " + error.message);
}
X
xsz233 已提交
198 199 200

try {
    appControl.getDisposedStatus(appId)
X
xsz233 已提交
201 202 203 204 205
        .then((data) => {
            console.info('getDisposedStatus success. DisposedStatus: ' + JSON.stringify(data));
        }).catch((error) => {
            console.error('getDisposedStatus failed ' + error.message);
        });
X
xsz233 已提交
206
} catch (error) {
X
xsz233 已提交
207
    console.error('getDisposedStatus failed ' + error.message);
X
xsz233 已提交
208 209 210 211 212
}
```

## appControl.getDisposedStatus

X
xsz233 已提交
213
getDisposedStatus(appId: string, callback: AsyncCallback\<Want>): void;
X
xsz233 已提交
214

X
xsz233 已提交
215
以异步方法获取指定应用的处置状态。使用callback异步回调,成功返回应用的处置状态,失败返回对应错误信息。
X
xsz233 已提交
216 217 218

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

X
xsz233 已提交
219
**系统能力:** SystemCapability.BundleManager.BundleFramework.AppControl
X
xsz233 已提交
220 221 222 223 224 225 226

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

**参数:**

| 名称          | 类型     | 必填   | 描述                                      |
| ----------- | ------ | ---- | --------------------------------------- |
X
xsz233 已提交
227
| appId  | string | 是    | 要查询的应用的appId<br> appId是应用的唯一标识,由应用的包名和签名信息决定,可以通过getBundleInfo接口获取。        |
X
xsz233 已提交
228
| callback    | AsyncCallback\<Want> | 是    | 回调函数。当获取应用的处置状态成功时,err为undefined,data为获取到的处置状态;否则为错误对象。                    |
X
xsz233 已提交
229

X
xsz233 已提交
230
**错误码**
X
xsz233 已提交
231

X
xsz233 已提交
232 233
以下错误码的详细介绍请参见[ohos.bundle错误码](../errorcodes/errcode-bundle.md)

X
xsz233 已提交
234 235 236 237 238 239
| 错误码 | 错误信息                                |
| ------ | -------------------------------------- |
| 17700005 |  The specified appId was not found.  |

**示例:**

X
xsz233 已提交
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
```ts
import appControl from '@ohos.bundle.appControl'
import bundleManager from '@ohos.bundle.bundleManager';

// 获取appId
var bundleName = 'com.example.myapplication';
var appId;
try {
    bundleManager.getBundleInfo(bundleName, BundleManager.BundleFlags.GET_BUNDLE_INFO_WITH_SIGNATURE_INFO)
        .then((data) => {
            appId = data.signatureInfo.appId;
        }, error => {
            console.error("getBundleInfo failed " + error.message);
        });
} catch (error) {
    console.error("getBundleInfo failed " + error.message);
}
X
xsz233 已提交
257 258 259 260

try {
    appControl.getDisposedStatus(appId, (err, data) => {
        if (err) {
X
xsz233 已提交
261
            console.error('getDisposedStatus failed ' + error.message);
X
xsz233 已提交
262 263 264 265 266
            return;
        }
        console.info('getDisposedStatus success. DisposedStatus: ' + JSON.stringify(data));
    });
} catch (error) {
X
xsz233 已提交
267
    console.error('getDisposedStatus failed ' + error.message);
X
xsz233 已提交
268 269 270 271 272
}
```

## appControl.deleteDisposedStatus

X
xsz233 已提交
273
deleteDisposedStatus(appId: string): Promise\<void>
X
xsz233 已提交
274

X
xsz233 已提交
275
以异步方法删除应用的处置状态。使用promise异步回调,成功返回null,失败返回对应错误信息。
X
xsz233 已提交
276 277 278 279 280 281 282 283 284 285 286

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

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

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

**参数:**

| 名称          | 类型     | 必填   | 描述                                      |
| ----------- | ------ | ---- | --------------------------------------- |
X
xsz233 已提交
287
| appId  | string | 是    | 要删除处置状态的应用的appId<br> appId是应用的唯一标识,由应用的包名和签名信息决定,可以通过getBundleInfo接口获取。       |                        |
X
xsz233 已提交
288 289 290 291 292 293

**返回值:**

| 类型                        | 说明                 |
| ------------------------- | ------------------ |
| Promise\<void> | Promise对象,无返回结果的Promise对象 |
X
xsz233 已提交
294

X
xsz233 已提交
295
**错误码**
X
xsz233 已提交
296

X
xsz233 已提交
297 298
以下错误码的详细介绍请参见[ohos.bundle错误码](../errorcodes/errcode-bundle.md)

X
xsz233 已提交
299 300 301 302 303 304
| 错误码 | 错误信息                                |
| ------ | -------------------------------------- |
| 17700005 |  The specified appId was not found.  |

**示例:**

X
xsz233 已提交
305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321
```ts
import appControl from '@ohos.bundle.appControl'
import bundleManager from '@ohos.bundle.bundleManager';

// 获取appId
var bundleName = 'com.example.myapplication';
var appId;
try {
    bundleManager.getBundleInfo(bundleName, BundleManager.BundleFlags.GET_BUNDLE_INFO_WITH_SIGNATURE_INFO)
        .then((data) => {
            appId = data.signatureInfo.appId;
        }, error => {
            console.error("getBundleInfo failed " + error.message);
        });
} catch (error) {
    console.error("getBundleInfo failed " + error.message);
}
X
xsz233 已提交
322 323

try {
X
xsz233 已提交
324 325 326 327 328 329
    appControl.deleteDisposedStatus(appId)
        .then(() => {
            console.info('deleteDisposedStatus success');
        }).catch((error) => {
            console.error('deleteDisposedStatus failed ' + error.message);
        });
X
xsz233 已提交
330
} catch (error) {
X
xsz233 已提交
331
    console.error('deleteDisposedStatus failed ' + error.message);
X
xsz233 已提交
332 333 334 335 336
}
```

## appControl.deleteDisposedStatus

X
xsz233 已提交
337
deleteDisposedStatus(appId: string, callback: AsyncCallback\<void>) : void
X
xsz233 已提交
338

X
xsz233 已提交
339
以异步方法删除应用的处置状态。使用callback异步回调,成功返回null,失败返回对应错误信息。
X
xsz233 已提交
340 341 342 343 344 345 346 347 348 349 350

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

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

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

**参数:**

| 名称          | 类型     | 必填   | 描述                                      |
| ----------- | ------ | ---- | --------------------------------------- |
X
xsz233 已提交
351 352
| appId  | string | 是    | 要查询的应用的appId。<br> appId是应用的唯一标识,由应用的包名和签名信息决定,可以通过getBundleInfo接口获取。       |
| callback    | AsyncCallback\<void> | 是    | 回调函数,当设置处置状态成功时,err返回undefined。否则回调函数返回具体错误对象。                   |
X
xsz233 已提交
353

X
xsz233 已提交
354
**错误码**
X
xsz233 已提交
355

X
xsz233 已提交
356 357
以下错误码的详细介绍请参见[ohos.bundle错误码](../errorcodes/errcode-bundle.md)

X
xsz233 已提交
358 359 360 361 362 363
| 错误码 | 错误信息                                |
| ------ | -------------------------------------- |
| 17700005 |  The specified appId was not found.  |

**示例:**

X
xsz233 已提交
364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380
```ts
import appControl from '@ohos.bundle.appControl'
import bundleManager from '@ohos.bundle.bundleManager';

// 获取appId
var bundleName = 'com.example.myapplication';
var appId;
try {
    bundleManager.getBundleInfo(bundleName, BundleManager.BundleFlags.GET_BUNDLE_INFO_WITH_SIGNATURE_INFO)
        .then((data) => {
            appId = data.signatureInfo.appId;
        }, error => {
            console.error("getBundleInfo failed " + error.message);
        });
} catch (error) {
    console.error("getBundleInfo failed " + error.message);
}
X
xsz233 已提交
381 382

try {
X
xsz233 已提交
383 384 385 386 387 388
    appControl.deleteDisposedStatus(appId, (err, data) => {
        if (err) {
            console.error('deleteDisposedStatus failed ' + error.message);
            return;
        }
        console.info('deleteDisposedStatus success');
X
xsz233 已提交
389 390
    });
} catch (error) {
X
xsz233 已提交
391
    console.error('deleteDisposedStatus failed ' + error.message);
X
xsz233 已提交
392
}
X
xsz233 已提交
393
```
X
xsz233 已提交
394