You need to sign in or sign up before continuing.
js-apis-defaultAppManager.md 23.7 KB
Newer Older
Z
zengyawen 已提交
1
# @ohos.bundle.defaultAppManager (默认应用管理)
2

3
本模块提供查询默认应用的能力,支持查询当前应用是否是默认应用。
4

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

## 导入模块

```
J
junyi233 已提交
12
import defaultAppMgr from '@ohos.bundle.defaultAppManager';
13
```
Z
zhaogan 已提交
14 15 16 17 18 19 20

## 权限列表

| 权限                                    | 权限等级    | 描述             |
| --------------------------------------- | ----------- | ---------------- |
| ohos.permission.GET_DEFAULT_APPLICATION | system_core | 默认应用相关权限。 |

J
junyi233 已提交
21
权限等级参考[权限等级说明](../../security/accesstoken-overview.md#权限等级说明)
Z
zhaogan 已提交
22 23


24 25
## defaultAppMgr.ApplicationType

X
xsz233 已提交
26
默认应用的应用类型。
27

Z
zhaogan 已提交
28
**系统能力:** SystemCapability.BundleManager.BundleFramework.DefaultApp
29

J
junyi233 已提交
30 31
| 名称   | 值 | 说明                                   |
| -------- | -------------------------------------- | -------------------------------------- |
J
junyi233 已提交
32 33 34 35 36 37 38 39
| BROWSER  | "Web Browser" | 默认浏览器。                            |
| IMAGE    | "Image Gallery" | 默认图片查看器。                         |
| AUDIO    | "Audio Player" | 默认音频播放器。                         |
| VIDEO    | "Video Player" | 默认视频播放器。                         |
| PDF      | "PDF Viewer" | 默认PDF文档查看器。                      |
| WORD     | "Word Viewer" | 默认WORD文档查看器。                     |
| EXCEL    | "Excel Viewer" | 默认EXCEL文档查看器。                    |
| PPT      | "PPT Viewer" | 默认PPT文档查看器。                      |
40 41 42 43 44

## defaultAppMgr.isDefaultApplication

isDefaultApplication(type: string): Promise\<boolean>

45
以异步方法根据系统已定义的应用类型判断当前应用是否是该应用类型的默认应用,使用Promise形式返回结果。
46

Z
zhaogan 已提交
47
**系统能力:** SystemCapability.BundleManager.BundleFramework.DefaultApp
48 49 50

**参数:**

J
junyi233 已提交
51
| 参数名         | 类型     | 必填   | 说明                                      |
52
| ----------- | ------ | ---- | --------------------------------------- |
53
| type  | string | 是    | 要查询的应用类型,取[ApplicationType](#defaultappmgrapplicationtype)中的值。                           |
54 55 56 57 58

**返回值:**

| 类型                        | 说明                 |
| ------------------------- | ------------------ |
59
| Promise\<boolean> | Promise形式返回当前应用是否是默认应用,true表示是默认应用,false表示不是默认应用。 |
60

Z
zhaogan 已提交
61

62 63
**示例:**

X
xsz233 已提交
64
```ts
W
wanghang 已提交
65
import defaultAppMgr from '@ohos.bundle.defaultAppManager';
66 67 68 69 70 71 72 73 74 75 76 77
defaultAppMgr.isDefaultApplication(defaultAppMgr.ApplicationType.BROWSER)
.then((data) => {
    console.info('Operation successful. IsDefaultApplication ? ' + JSON.stringify(data));
}).catch((error) => {
    console.error('Operation failed. Cause: ' + JSON.stringify(error));
});
```

## defaultAppMgr.isDefaultApplication

isDefaultApplication(type: string, callback: AsyncCallback\<boolean>): void

78
以异步方法根据系统已定义的应用类型判断当前应用是否是该应用类型的默认应用,使用callback形式返回结果。
79

Z
zhaogan 已提交
80
**系统能力:** SystemCapability.BundleManager.BundleFramework.DefaultApp
81 82 83

**参数:**

J
junyi233 已提交
84
| 参数名         | 类型                              | 必填   | 说明                                      |
85
| ----------- | ------------------------------- | ---- | --------------------------------------- |
86 87
| type  | string                          | 是    | 要查询的应用类型,取[ApplicationType](#defaultappmgrapplicationtype)中的值。                            |
| callback    | AsyncCallback\<boolean> | 是    | 程序启动作为入参的回调函数,返回当前应用是否是默认应用,true表示是默认应用,false表示不是默认应用。 |
88 89 90

**示例:**

X
xsz233 已提交
91
```ts
W
wanghang 已提交
92
import defaultAppMgr from '@ohos.bundle.defaultAppManager';
93 94 95 96 97 98 99 100 101
defaultAppMgr.isDefaultApplication(defaultAppMgr.ApplicationType.BROWSER, (err, data) => {
    if (err) {
        console.error('Operation failed. Cause: ' + JSON.stringify(err));
        return;
    }
    console.info('Operation successful. IsDefaultApplication ? ' + JSON.stringify(data));
 });
```

102 103 104 105
## defaultAppMgr.getDefaultApplication

getDefaultApplication(type: string, userId?: number): Promise\<BundleInfo>

106 107
以异步方法根据系统已定义的应用类型或者符合媒体类型格式(type/subtype)的文件类型获取默认应用信息,使用Promise形式返回结果。

108
**需要权限:** ohos.permission.GET_DEFAULT_APPLICATION
109

Z
zhaogan 已提交
110
**系统能力:** SystemCapability.BundleManager.BundleFramework.DefaultApp
111

J
junyi233 已提交
112
**系统API:**  此接口为系统接口,三方应用不支持调用
113 114 115

**参数:**

J
junyi233 已提交
116
| 参数名         | 类型     | 必填   | 说明                                      |
117
| ----------- | ------ | ---- | --------------------------------------- |
118
| type  | string | 是    | 要查询的应用类型,取[ApplicationType](#defaultappmgrapplicationtype)中的值,或者符合媒体类型格式的文件类型。       |
119
| userId  | number | 否    | 用户ID。默认值:调用方所在用户。                        |
120 121 122 123 124

**返回值:**

| 类型                        | 说明                 |
| ------------------------- | ------------------ |
125
| Promise\<[BundleInfo](js-apis-bundle-BundleInfo.md)> | Promise形式返回默认应用包信息。 |
126

127 128
**错误码:**

Z
zhaogan 已提交
129 130
以下错误码的详细介绍请参见[ohos.bundle错误码](../errorcodes/errorcode-bundle.md)

J
junyi233 已提交
131
| 错误码ID | 错误信息                                  |
132
| -------- | ----------------------------------------- |
J
junyi233 已提交
133
| 17700004 | The specified user ID is not found.       |
134 135 136
| 17700023 | The specified default app does not exist. |
| 17700025 | The specified type is invalid.            |

137 138
**示例:**

X
xsz233 已提交
139
```ts
W
wanghang 已提交
140
import defaultAppMgr from '@ohos.bundle.defaultAppManager';
141 142
defaultAppMgr.getDefaultApplication(defaultAppMgr.ApplicationType.BROWSER)
.then((data) => {
143 144 145
    console.info('Operation successful. bundleInfo: ' + JSON.stringify(data));
})
.catch((error) => {
146 147 148
    console.error('Operation failed. Cause: ' + JSON.stringify(error));
});

149
defaultAppMgr.getDefaultApplication("image/png")
150
.then((data) => {
151 152 153
    console.info('Operation successful. bundleInfo: ' + JSON.stringify(data));
})
.catch((error) => {
154 155 156 157
    console.error('Operation failed. Cause: ' + JSON.stringify(error));
});
```

158 159 160 161 162
## defaultAppMgr.getDefaultApplication

getDefaultApplication(type: string, userId: number, callback: AsyncCallback\<BundleInfo>) : void

以异步方法根据系统已定义的应用类型或者符合媒体类型格式(type/subtype)的文件类型获取默认应用信息,使用callback形式返回结果。
163

164
**需要权限:** ohos.permission.GET_DEFAULT_APPLICATION
165

Z
zhaogan 已提交
166
**系统能力:** SystemCapability.BundleManager.BundleFramework.DefaultApp
167

J
junyi233 已提交
168
**系统API:**  此接口为系统接口,三方应用不支持调用
169 170 171

**参数:**

J
junyi233 已提交
172
| 参数名         | 类型     | 必填   | 说明                                      |
173
| ----------- | ------ | ---- | --------------------------------------- |
174
| type  | string | 是    | 要查询的应用类型,取[ApplicationType](#defaultappmgrapplicationtype)中的值,或者符合媒体类型格式的文件类型。       |
175 176
| userId  | number | 是    | 用户ID。                           |
| callback    | AsyncCallback\<[BundleInfo](js-apis-bundle-BundleInfo.md)> | 是    | 程序启动作为入参的回调函数,返回包信息。                    |
177

178 179
**错误码:**

Z
zhaogan 已提交
180 181
以下错误码的详细介绍请参见[ohos.bundle错误码](../errorcodes/errorcode-bundle.md)

J
junyi233 已提交
182
| 错误码ID | 错误信息                                  |
183
| -------- | ----------------------------------------- |
J
junyi233 已提交
184
| 17700004 | The specified user ID is not found.       |
185 186 187
| 17700023 | The specified default app does not exist. |
| 17700025 | The specified type is invalid.            |

188 189
**示例:**

X
xsz233 已提交
190
```ts
W
wanghang 已提交
191 192 193
import defaultAppMgr from '@ohos.bundle.defaultAppManager';
let userId = 100;
defaultAppMgr.getDefaultApplication(defaultAppMgr.ApplicationType.BROWSER, userId, (err, data) => {
194 195 196 197 198 199 200
    if (err) {
        console.error('Operation failed. Cause: ' + JSON.stringify(err));
        return;
    }
    console.info('Operation successful. bundleInfo:' + JSON.stringify(data));
});

W
wanghang 已提交
201
defaultAppMgr.getDefaultApplication("image/png", userId, (err, data) => {
202 203 204 205 206
    if (err) {
        console.error('Operation failed. Cause: ' + JSON.stringify(err));
        return;
    }
    console.info('Operation successful. bundleInfo:' + JSON.stringify(data));
207 208
});
```
209 210 211 212 213 214 215

## defaultAppMgr.getDefaultApplication

getDefaultApplication(type: string, callback: AsyncCallback\<BundleInfo>) : void

以异步方法根据系统已定义的应用类型或者符合媒体类型格式(type/subtype)的文件类型获取默认应用信息,使用callback形式返回结果。

216
**需要权限:** ohos.permission.GET_DEFAULT_APPLICATION
217

Z
zhaogan 已提交
218
**系统能力:** SystemCapability.BundleManager.BundleFramework.DefaultApp
219

J
junyi233 已提交
220
**系统API:**  此接口为系统接口,三方应用不支持调用
221 222 223

**参数:**

J
junyi233 已提交
224
| 参数名         | 类型     | 必填   | 说明                                      |
225
| ----------- | ------ | ---- | --------------------------------------- |
226
| type  | string | 是    | 要查询的应用类型,取[ApplicationType](#defaultappmgrapplicationtype)中的值,或者符合媒体类型格式的文件类型。       |
227 228
| callback    | AsyncCallback\<[BundleInfo](js-apis-bundle-BundleInfo.md)> | 是    | 程序启动作为入参的回调函数,返回包信息。                    |

229 230
**错误码:**

Z
zhaogan 已提交
231 232
以下错误码的详细介绍请参见[ohos.bundle错误码](../errorcodes/errorcode-bundle.md)

J
junyi233 已提交
233
| 错误码ID | 错误信息                                  |
234
| -------- | ----------------------------------------- |
J
junyi233 已提交
235
| 17700004 | The specified user ID is not found.       |
236 237 238
| 17700023 | The specified default app does not exist. |
| 17700025 | The specified type is invalid.            |

239 240
**示例:**

X
xsz233 已提交
241
```ts
W
wanghang 已提交
242
import defaultAppMgr from '@ohos.bundle.defaultAppManager';
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
defaultAppMgr.getDefaultApplication(defaultAppMgr.ApplicationType.BROWSER, (err, data) => {
    if (err) {
        console.error('Operation failed. Cause: ' + JSON.stringify(err));
        return;
    }
    console.info('Operation successful. bundleInfo:' + JSON.stringify(data));
});
defaultAppMgr.getDefaultApplication("image/png", (err, data) => {
    if (err) {
        console.error('Operation failed. Cause: ' + JSON.stringify(err));
        return;
    }
    console.info('Operation successful. bundleInfo:' + JSON.stringify(data));
});
```

## defaultAppMgr.setDefaultApplication

J
junyi233 已提交
261
setDefaultApplication(type: string, elementName: ElementName, userId?: number): Promise\<void>
262

263
以异步方法根据系统已定义的应用类型或者符合媒体类型格式(type/subtype)的文件类型设置默认应用,使用Promise形式返回结果。
264

265
**需要权限:** ohos.permission.SET_DEFAULT_APPLICATION
266

Z
zhaogan 已提交
267
**系统能力:** SystemCapability.BundleManager.BundleFramework.DefaultApp
268

J
junyi233 已提交
269
**系统API:**  此接口为系统接口,三方应用不支持调用
270 271 272

**参数:**

J
junyi233 已提交
273
| 参数名         | 类型     | 必填   | 说明                                      |
274
| ----------- | ------ | ---- | --------------------------------------- |
275
| type  | string | 是    | 要设置的应用类型,取[ApplicationType](#defaultappmgrapplicationtype)中的值,或者符合媒体类型格式的文件类型。       |
276 277 278
| elementName  | [ElementName](js-apis-bundle-ElementName.md) | 是    | 要设置为默认应用的组件信息。                           |
| userId  | number | 否    | 用户ID。默认值:调用方所在用户。                           |

W
wanghang 已提交
279 280 281 282
**返回值:**

| 类型           | 说明                               |
| -------------- | ---------------------------------- |
283
| Promise\<void> | Promise对象,无返回结果的Promise对象。 |
W
wanghang 已提交
284

285 286
**错误码:**

Z
zhaogan 已提交
287 288
以下错误码的详细介绍请参见[ohos.bundle错误码](../errorcodes/errorcode-bundle.md)

J
junyi233 已提交
289
| 错误码ID | 错误信息                                       |
290
| -------- | ---------------------------------------------- |
J
junyi233 已提交
291
| 17700004 | The specified user ID is not found.            |
292
| 17700025 | The specified type is invalid.                 |
J
junyi233 已提交
293
| 17700028 | The specified ability does not match the type. |
294

295 296
**示例:**

X
xsz233 已提交
297
```ts
W
wanghang 已提交
298
import defaultAppMgr from '@ohos.bundle.defaultAppManager';
299
defaultAppMgr.setDefaultApplication(defaultAppMgr.ApplicationType.BROWSER, {
300
    bundleName: "com.example.myapplication",
301
    moduleName: "module01",
302
    abilityName: "EntryAbility"
W
wanghang 已提交
303
}).then((data) => {
304
    console.info('Operation successful.');
W
wanghang 已提交
305 306 307 308 309 310
}).catch((error) => {
    console.error('Operation failed. Cause: ' + JSON.stringify(error));
});

let userId = 100;
defaultAppMgr.setDefaultApplication(defaultAppMgr.ApplicationType.BROWSER, {
311
    bundleName: "com.example.myapplication",
W
wanghang 已提交
312
    moduleName: "module01",
313
    abilityName: "EntryAbility"
W
wanghang 已提交
314 315 316
}, userId).then((data) => {
    console.info('Operation successful.');
}).catch((error) => {
317 318 319 320
    console.error('Operation failed. Cause: ' + JSON.stringify(error));
});

defaultAppMgr.setDefaultApplication("image/png", {
321
    bundleName: "com.example.myapplication",
322
    moduleName: "module01",
323
    abilityName: "EntryAbility"
W
wanghang 已提交
324
}, userId).then((data) => {
325
    console.info('Operation successful.');
W
wanghang 已提交
326
}).catch((error) => {
327 328 329 330 331 332 333 334
    console.error('Operation failed. Cause: ' + JSON.stringify(error));
});
```

## defaultAppMgr.setDefaultApplication

setDefaultApplication(type: string, elementName: ElementName, userId: number, callback: AsyncCallback\<void>) : void;

335
以异步方法根据系统已定义的应用类型或者符合媒体类型格式(type/subtype)的文件类型设置默认应用,使用callback形式返回结果。
336

337
**需要权限:** ohos.permission.SET_DEFAULT_APPLICATION
338

Z
zhaogan 已提交
339
**系统能力:** SystemCapability.BundleManager.BundleFramework.DefaultApp
340

J
junyi233 已提交
341
**系统API:**  此接口为系统接口,三方应用不支持调用
342 343 344

**参数:**

J
junyi233 已提交
345
| 参数名         | 类型     | 必填   | 说明                                      |
346
| ----------- | ------ | ---- | --------------------------------------- |
347
| type  | string | 是    | 要设置的应用类型,取[ApplicationType](#defaultappmgrapplicationtype)中的值,或者符合媒体类型格式的文件类型。       |
348 349 350 351
| elementName  | [ElementName](js-apis-bundle-ElementName.md) | 是    | 要设置为默认应用的组件信息。                           |
| userId  | number | 是    | 用户ID。                           |
| callback    | AsyncCallback\<void> | 是    | 程序启动作为入参的回调函数。                    |

352 353
**错误码:**

Z
zhaogan 已提交
354 355
以下错误码的详细介绍请参见[ohos.bundle错误码](../errorcodes/errorcode-bundle.md)

J
junyi233 已提交
356
| 错误码ID | 错误信息                                       |
357
| -------- | ---------------------------------------------- |
J
junyi233 已提交
358
| 17700004 | The specified user ID is not found.            |
359
| 17700025 | The specified type is invalid.                 |
J
junyi233 已提交
360
| 17700028 | The specified ability does not match the type. |
361

362 363
**示例:**

X
xsz233 已提交
364
```ts
W
wanghang 已提交
365 366
import defaultAppMgr from '@ohos.bundle.defaultAppManager';
let userId = 100;
367
defaultAppMgr.setDefaultApplication(defaultAppMgr.ApplicationType.BROWSER, {
368
    bundleName: "com.example.myapplication",
369
    moduleName: "module01",
370
    abilityName: "EntryAbility"
W
wanghang 已提交
371
}, userId, (err, data) => {
372 373 374 375 376 377 378 379
    if (err) {
        console.error('Operation failed. Cause: ' + JSON.stringify(err));
        return;
    }
    console.info('Operation successful.');
 });

defaultAppMgr.setDefaultApplication("image/png", {
380
    bundleName: "com.example.myapplication",
381
    moduleName: "module01",
382
    abilityName: "EntryAbility"
W
wanghang 已提交
383
}, userId, (err, data) => {
384 385 386 387 388 389 390 391 392 393 394 395
    if (err) {
        console.error('Operation failed. Cause: ' + JSON.stringify(err));
        return;
    }
    console.info('Operation successful.');
 });
```

## defaultAppMgr.setDefaultApplication

setDefaultApplication(type: string, elementName: ElementName, callback: AsyncCallback\<void>) : void;

396
以异步方法根据系统已定义的应用类型或者符合媒体类型格式(type/subtype)的文件类型设置默认应用,使用callback形式返回结果。
397

398
**需要权限:** ohos.permission.SET_DEFAULT_APPLICATION
399

Z
zhaogan 已提交
400
**系统能力:** SystemCapability.BundleManager.BundleFramework.DefaultApp
401

J
junyi233 已提交
402
**系统API:**  此接口为系统接口,三方应用不支持调用
403 404 405

**参数:**

J
junyi233 已提交
406
| 参数名         | 类型     | 必填   | 说明                                      |
407
| ----------- | ------ | ---- | --------------------------------------- |
408
| type  | string | 是    | 要设置的应用类型,取[ApplicationType](#defaultappmgrapplicationtype)中的值,或者符合媒体类型格式的文件类型。       |
409 410 411
| elementName  | [ElementName](js-apis-bundle-ElementName.md) | 是    | 要设置为默认应用的组件信息。                           |
| callback    | AsyncCallback\<void> | 是    | 程序启动作为入参的回调函数。                    |

412 413
**错误码:**

Z
zhaogan 已提交
414 415
以下错误码的详细介绍请参见[ohos.bundle错误码](../errorcodes/errorcode-bundle.md)

J
junyi233 已提交
416
| 错误码ID | 错误信息                                       |
417
| -------- | ---------------------------------------------- |
J
junyi233 已提交
418
| 17700004 | The specified user ID is not found.            |
419
| 17700025 | The specified type is invalid.                 |
J
junyi233 已提交
420
| 17700028 | The specified ability does not match the type. |
421

422 423
**示例:**

X
xsz233 已提交
424
```ts
W
wanghang 已提交
425
import defaultAppMgr from '@ohos.bundle.defaultAppManager';
426
defaultAppMgr.setDefaultApplication(defaultAppMgr.ApplicationType.BROWSER, {
427
    bundleName: "com.example.myapplication",
428
    moduleName: "module01",
429
    abilityName: "EntryAbility"
430 431 432 433 434 435 436 437 438
}, (err, data) => {
    if (err) {
        console.error('Operation failed. Cause: ' + JSON.stringify(err));
        return;
    }
    console.info('Operation successful.');
 });

defaultAppMgr.setDefaultApplication("image/png", {
439
    bundleName: "com.example.myapplication",
440
    moduleName: "module01",
441
    abilityName: "EntryAbility"
442 443 444 445 446 447 448 449 450 451 452 453 454
}, (err, data) => {
    if (err) {
        console.error('Operation failed. Cause: ' + JSON.stringify(err));
        return;
    }
    console.info('Operation successful.');
 });
```

## defaultAppMgr.resetDefaultApplication

resetDefaultApplication(type: string, userId?: number): Promise\<void>

455
以异步方法根据系统已定义的应用类型或者符合媒体类型格式(type/subtype)的文件类型重置默认应用,使用Promise形式返回结果。
456

457
**需要权限:** ohos.permission.SET_DEFAULT_APPLICATION
458

Z
zhaogan 已提交
459
**系统能力:** SystemCapability.BundleManager.BundleFramework.DefaultApp
460

J
junyi233 已提交
461
**系统API:**  此接口为系统接口,三方应用不支持调用
462 463 464

**参数:**

J
junyi233 已提交
465
| 参数名         | 类型     | 必填   | 说明                                      |
466
| ----------- | ------ | ---- | --------------------------------------- |
467
| type  | string | 是    | 要重置的应用类型,取[ApplicationType](#defaultappmgrapplicationtype)中的值,或者符合媒体类型格式的文件类型。       |
468 469
| userId  | number | 否    | 用户ID。默认值:调用方所在用户。                           |

470 471
**错误码:**

Z
zhaogan 已提交
472 473
以下错误码的详细介绍请参见[ohos.bundle错误码](../errorcodes/errorcode-bundle.md)

J
junyi233 已提交
474
| 错误码ID | 错误信息                            |
475
| -------- | ----------------------------------- |
J
junyi233 已提交
476
| 17700004 | The specified user ID is not found. |
477 478
| 17700025 | The specified type is invalid.      |

479 480
**示例:**

X
xsz233 已提交
481
```ts
W
wanghang 已提交
482 483 484
import defaultAppMgr from '@ohos.bundle.defaultAppManager';
let userId = 100;
defaultAppMgr.resetDefaultApplication(defaultAppMgr.ApplicationType.BROWSER, userId)
485 486 487 488 489 490 491
.then((data) => {
    console.info('Operation successful.');
})
.catch((error) => {
    console.error('Operation failed. Cause: ' + JSON.stringify(error));
});

W
wanghang 已提交
492
defaultAppMgr.resetDefaultApplication("image/png", userId)
493 494 495 496 497 498 499 500 501 502 503 504
.then((data) => {
    console.info('Operation successful.');
})
.catch((error) => {
    console.error('Operation failed. Cause: ' + JSON.stringify(error));
});
```

## defaultAppMgr.resetDefaultApplication

resetDefaultApplication(type: string, userId: number, callback: AsyncCallback\<void>) : void;

505
以异步方法根据系统已定义的应用类型或者符合媒体类型格式(type/subtype)的文件类型重置默认应用,使用callback形式返回结果。
506

507
**需要权限:** ohos.permission.SET_DEFAULT_APPLICATION
508

Z
zhaogan 已提交
509
**系统能力:** SystemCapability.BundleManager.BundleFramework.DefaultApp
510

J
junyi233 已提交
511
**系统API:**  此接口为系统接口,三方应用不支持调用
512 513 514

**参数:**

J
junyi233 已提交
515
| 参数名         | 类型     | 必填   | 说明                                      |
516
| ----------- | ------ | ---- | --------------------------------------- |
517
| type  | string | 是    | 要重置的应用类型,取[ApplicationType](#defaultappmgrapplicationtype)中的值,或者符合媒体类型格式的文件类型。       |
518 519 520
| userId  | number | 是    | 用户ID。                          |
| callback    | AsyncCallback\<void> | 是    | 程序启动作为入参的回调函数。                    |

521 522
**错误码:**

Z
zhaogan 已提交
523 524
以下错误码的详细介绍请参见[ohos.bundle错误码](../errorcodes/errorcode-bundle.md)

J
junyi233 已提交
525
| 错误码ID | 错误信息                            |
526
| -------- | ----------------------------------- |
J
junyi233 已提交
527
| 17700004 | The specified user ID is not found. |
528 529
| 17700025 | The specified type is invalid.      |

530 531
**示例:**

X
xsz233 已提交
532
```ts
W
wanghang 已提交
533 534 535
import defaultAppMgr from '@ohos.bundle.defaultAppManager';
let userId = 100;
defaultAppMgr.resetDefaultApplication(defaultAppMgr.ApplicationType.BROWSER, userId, (err, data) => {
536 537 538 539 540 541 542
    if (err) {
        console.error('Operation failed. Cause: ' + JSON.stringify(err));
        return;
    }
    console.info('Operation successful.');
});

W
wanghang 已提交
543
defaultAppMgr.resetDefaultApplication("image/png", userId, (err, data) => {
544 545 546 547 548 549 550 551 552 553 554 555
    if (err) {
        console.error('Operation failed. Cause: ' + JSON.stringify(err));
        return;
    }
    console.info('Operation successful.');
});
```

## defaultAppMgr.resetDefaultApplication

resetDefaultApplication(type: string, callback: AsyncCallback\<void>) : void;

556
以异步方法根据系统已定义的应用类型或者符合媒体类型格式(type/subtype)的文件类型重置默认应用,使用callback形式返回结果。
557

558
**需要权限:** ohos.permission.SET_DEFAULT_APPLICATION
559

Z
zhaogan 已提交
560
**系统能力:** SystemCapability.BundleManager.BundleFramework.DefaultApp
561

J
junyi233 已提交
562
**系统API:**  此接口为系统接口,三方应用不支持调用
563 564 565

**参数:**

J
junyi233 已提交
566
| 参数名         | 类型     | 必填   | 说明                                      |
567
| ----------- | ------ | ---- | --------------------------------------- |
568
| type  | string | 是    | 要重置的应用类型,取[ApplicationType](#defaultappmgrapplicationtype)中的值,或者符合媒体类型格式的文件类型。       |
569 570
| callback    | AsyncCallback\<void> | 是    | 程序启动作为入参的回调函数。                    |

571 572
**错误码:**

Z
zhaogan 已提交
573 574
以下错误码的详细介绍请参见[ohos.bundle错误码](../errorcodes/errorcode-bundle.md)

J
junyi233 已提交
575
| 错误码ID | 错误信息                            |
576
| -------- | ----------------------------------- |
J
junyi233 已提交
577
| 17700004 | The specified user ID is not found. |
578 579
| 17700025 | The specified type is invalid.      |

580 581
**示例:**

X
xsz233 已提交
582
```ts
W
wanghang 已提交
583
import defaultAppMgr from '@ohos.bundle.defaultAppManager';
584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599
defaultAppMgr.resetDefaultApplication(defaultAppMgr.ApplicationType.BROWSER, (err, data) => {
    if (err) {
        console.error('Operation failed. Cause: ' + JSON.stringify(err));
        return;
    }
    console.info('Operation successful.');
});

defaultAppMgr.resetDefaultApplication("image/png", (err, data) => {
    if (err) {
        console.error('Operation failed. Cause: ' + JSON.stringify(err));
        return;
    }
    console.info('Operation successful.');
});
```