• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# @ohos.bundle.defaultAppManager (Default Application Management)
2
3The **DefaultAppManager** module provides APIs to query whether the current application is the default application of a specific type.
4
5> **NOTE**
6>
7> The initial APIs of this module are supported since API version 9. Newly added APIs will be marked with a superscript to indicate their earliest API version.
8
9## Modules to Import
10
11```
12import defaultAppMgr from '@ohos.bundle.defaultAppManager';
13```
14
15## Required Permissions
16
17| Permission                                   | Permission Level   | Description            |
18| --------------------------------------- | ----------- | ---------------- |
19| ohos.permission.GET_DEFAULT_APPLICATION | system_core | Permission related to the default application.|
20
21For details, see [Permission Levels](../../security/accesstoken-overview.md#permission-levels).
22
23
24## defaultAppMgr.ApplicationType
25
26Enumerates the default application types.
27
28**System capability**: SystemCapability.BundleManager.BundleFramework.DefaultApp
29
30| Name  | Value| Description                                  |
31| -------- | -------------------------------------- | -------------------------------------- |
32| BROWSER  | "Web Browser" | Default browser.                           |
33| IMAGE    | "Image Gallery" | Default image viewer.                        |
34| AUDIO    | "Audio Player" | Default audio player.                        |
35| VIDEO    | "Video Player" | Default video player.                        |
36| PDF      | "PDF Viewer" | Default PDF reader.                     |
37| WORD     | "Word Viewer" | Default Word viewer.                    |
38| EXCEL    | "Excel Viewer" | Default Excel viewer.                   |
39| PPT      | "PPT Viewer" | Default PowerPoint viewer.                     |
40
41## defaultAppMgr.isDefaultApplication
42
43isDefaultApplication(type: string): Promise\<boolean>
44
45Checks whether this application is the default application of a system-defined application type. This API uses a promise to return the result.
46
47**System capability**: SystemCapability.BundleManager.BundleFramework.DefaultApp
48
49**Parameters**
50
51| Name        | Type    | Mandatory  | Description                                     |
52| ----------- | ------ | ---- | --------------------------------------- |
53| type  | string | Yes   | Type of the target application. It must be set to a value defined by [ApplicationType](#defaultappmgrapplicationtype).                          |
54
55**Return value**
56
57| Type                       | Description                |
58| ------------------------- | ------------------ |
59| Promise\<boolean> | Promise used to return the result. If the application is the default application, `true` is returned; otherwise, `false` is returned.|
60
61
62**Example**
63
64```ts
65import defaultAppMgr from '@ohos.bundle.defaultAppManager';
66defaultAppMgr.isDefaultApplication(defaultAppMgr.ApplicationType.BROWSER)
67.then((data) => {
68    console.info('Operation successful. IsDefaultApplication ? ' + JSON.stringify(data));
69}).catch((error) => {
70    console.error('Operation failed. Cause: ' + JSON.stringify(error));
71});
72```
73
74## defaultAppMgr.isDefaultApplication
75
76isDefaultApplication(type: string, callback: AsyncCallback\<boolean>): void
77
78Checks whether this application is the default application of a system-defined application type. This API uses an asynchronous callback to return the result.
79
80**System capability**: SystemCapability.BundleManager.BundleFramework.DefaultApp
81
82**Parameters**
83
84| Name        | Type                             | Mandatory  | Description                                     |
85| ----------- | ------------------------------- | ---- | --------------------------------------- |
86| type  | string                          | Yes   | Type of the target application. It must be set to a value defined by [ApplicationType](#defaultappmgrapplicationtype).                           |
87| callback    | AsyncCallback\<boolean> | Yes   | Callback used to return the result. If the application is the default application, `true` is returned; otherwise, `false` is returned.|
88
89**Example**
90
91```ts
92import defaultAppMgr from '@ohos.bundle.defaultAppManager';
93defaultAppMgr.isDefaultApplication(defaultAppMgr.ApplicationType.BROWSER, (err, data) => {
94    if (err) {
95        console.error('Operation failed. Cause: ' + JSON.stringify(err));
96        return;
97    }
98    console.info('Operation successful. IsDefaultApplication ? ' + JSON.stringify(data));
99 });
100```
101
102## defaultAppMgr.getDefaultApplication
103
104getDefaultApplication(type: string, userId?: number): Promise\<BundleInfo>
105
106Obtains the default application based on a system-defined application type or a file type that complies with the media type format (either specified by **type** or **subtype**). This API uses a promise to return the result.
107
108**Required permissions**: ohos.permission.GET_DEFAULT_APPLICATION
109
110**System capability**: SystemCapability.BundleManager.BundleFramework.DefaultApp
111
112**System API**: This is a system API and cannot be called by third-party applications.
113
114**Parameters**
115
116| Name        | Type    | Mandatory  | Description                                     |
117| ----------- | ------ | ---- | --------------------------------------- |
118| type  | string | Yes   | Type of the target application. It must be set to a value defined by [ApplicationType](#defaultappmgrapplicationtype) or a file type that complies with the media type format.      |
119| userId  | number | No   | User ID. The default value is the user ID of the caller.                        |
120
121**Return value**
122
123| Type                       | Description                |
124| ------------------------- | ------------------ |
125| Promise\<[BundleInfo](js-apis-bundle-BundleInfo.md)> | Promise used to return the default application.|
126
127**Error codes**
128
129For details about the error codes, see [Bundle Error Codes](../errorcodes/errorcode-bundle.md).
130
131| ID| Error Message                                 |
132| -------- | ----------------------------------------- |
133| 17700004 | The specified user ID is not found.       |
134| 17700023 | The specified default app does not exist. |
135| 17700025 | The specified type is invalid.            |
136
137**Example**
138
139```ts
140import defaultAppMgr from '@ohos.bundle.defaultAppManager';
141defaultAppMgr.getDefaultApplication(defaultAppMgr.ApplicationType.BROWSER)
142.then((data) => {
143    console.info('Operation successful. bundleInfo: ' + JSON.stringify(data));
144})
145.catch((error) => {
146    console.error('Operation failed. Cause: ' + JSON.stringify(error));
147});
148
149defaultAppMgr.getDefaultApplication("image/png")
150.then((data) => {
151    console.info('Operation successful. bundleInfo: ' + JSON.stringify(data));
152})
153.catch((error) => {
154    console.error('Operation failed. Cause: ' + JSON.stringify(error));
155});
156```
157
158## defaultAppMgr.getDefaultApplication
159
160getDefaultApplication(type: string, userId: number, callback: AsyncCallback\<BundleInfo>) : void
161
162Obtains the default application of a user based on a system-defined application type or a file type that complies with the media type format (either specified by **type** or **subtype**). This API uses an asynchronous callback to return the result.
163
164**Required permissions**: ohos.permission.GET_DEFAULT_APPLICATION
165
166**System capability**: SystemCapability.BundleManager.BundleFramework.DefaultApp
167
168**System API**: This is a system API and cannot be called by third-party applications.
169
170**Parameters**
171
172| Name        | Type    | Mandatory  | Description                                     |
173| ----------- | ------ | ---- | --------------------------------------- |
174| type  | string | Yes   | Type of the target application. It must be set to a value defined by [ApplicationType](#defaultappmgrapplicationtype) or a file type that complies with the media type format.      |
175| userId  | number | Yes   | User ID.                          |
176| callback    | AsyncCallback\<[BundleInfo](js-apis-bundle-BundleInfo.md)> | Yes   | Callback used to return the default application.                   |
177
178**Error codes**
179
180For details about the error codes, see [Bundle Error Codes](../errorcodes/errorcode-bundle.md).
181
182| ID| Error Message                                 |
183| -------- | ----------------------------------------- |
184| 17700004 | The specified user ID is not found.       |
185| 17700023 | The specified default app does not exist. |
186| 17700025 | The specified type is invalid.            |
187
188**Example**
189
190```ts
191import defaultAppMgr from '@ohos.bundle.defaultAppManager';
192let userId = 100;
193defaultAppMgr.getDefaultApplication(defaultAppMgr.ApplicationType.BROWSER, userId, (err, data) => {
194    if (err) {
195        console.error('Operation failed. Cause: ' + JSON.stringify(err));
196        return;
197    }
198    console.info('Operation successful. bundleInfo:' + JSON.stringify(data));
199});
200
201defaultAppMgr.getDefaultApplication("image/png", userId, (err, data) => {
202    if (err) {
203        console.error('Operation failed. Cause: ' + JSON.stringify(err));
204        return;
205    }
206    console.info('Operation successful. bundleInfo:' + JSON.stringify(data));
207});
208```
209
210## defaultAppMgr.getDefaultApplication
211
212getDefaultApplication(type: string, callback: AsyncCallback\<BundleInfo>) : void
213
214Obtains the default application based on a system-defined application type or a file type that complies with the media type format (either specified by **type** or **subtype**). This API uses an asynchronous callback to return the result.
215
216**Required permissions**: ohos.permission.GET_DEFAULT_APPLICATION
217
218**System capability**: SystemCapability.BundleManager.BundleFramework.DefaultApp
219
220**System API**: This is a system API and cannot be called by third-party applications.
221
222**Parameters**
223
224| Name        | Type    | Mandatory  | Description                                     |
225| ----------- | ------ | ---- | --------------------------------------- |
226| type  | string | Yes   | Type of the target application. It must be set to a value defined by [ApplicationType](#defaultappmgrapplicationtype) or a file type that complies with the media type format.      |
227| callback    | AsyncCallback\<[BundleInfo](js-apis-bundle-BundleInfo.md)> | Yes   | Callback used to return the default application.                   |
228
229**Error codes**
230
231For details about the error codes, see [Bundle Error Codes](../errorcodes/errorcode-bundle.md).
232
233| ID| Error Message                                 |
234| -------- | ----------------------------------------- |
235| 17700004 | The specified user ID is not found.       |
236| 17700023 | The specified default app does not exist. |
237| 17700025 | The specified type is invalid.            |
238
239**Example**
240
241```ts
242import defaultAppMgr from '@ohos.bundle.defaultAppManager';
243defaultAppMgr.getDefaultApplication(defaultAppMgr.ApplicationType.BROWSER, (err, data) => {
244    if (err) {
245        console.error('Operation failed. Cause: ' + JSON.stringify(err));
246        return;
247    }
248    console.info('Operation successful. bundleInfo:' + JSON.stringify(data));
249});
250defaultAppMgr.getDefaultApplication("image/png", (err, data) => {
251    if (err) {
252        console.error('Operation failed. Cause: ' + JSON.stringify(err));
253        return;
254    }
255    console.info('Operation successful. bundleInfo:' + JSON.stringify(data));
256});
257```
258
259## defaultAppMgr.setDefaultApplication
260
261setDefaultApplication(type: string, elementName: ElementName, userId?: number): Promise\<void>
262
263Sets the default application based on a system-defined application type or a file type that complies with the media type format (either specified by **type** or **subtype**). This API uses a promise to return the result.
264
265**Required permissions**: ohos.permission.SET_DEFAULT_APPLICATION
266
267**System capability**: SystemCapability.BundleManager.BundleFramework.DefaultApp
268
269**System API**: This is a system API and cannot be called by third-party applications.
270
271**Parameters**
272
273| Name        | Type    | Mandatory  | Description                                     |
274| ----------- | ------ | ---- | --------------------------------------- |
275| type  | string | Yes   | Type of the target application. It must be set to a value defined by [ApplicationType](#defaultappmgrapplicationtype) or a file type that complies with the media type format.      |
276| elementName  | [ElementName](js-apis-bundle-ElementName.md) | Yes   | Information about the element to be set as the default application.                          |
277| userId  | number | No   | User ID. The default value is the user ID of the caller.                           |
278
279**Return value**
280
281| Type          | Description                              |
282| -------------- | ---------------------------------- |
283| Promise\<void> | Promise that returns no value.|
284
285**Error codes**
286
287For details about the error codes, see [Bundle Error Codes](../errorcodes/errorcode-bundle.md).
288
289| ID| Error Message                                      |
290| -------- | ---------------------------------------------- |
291| 17700004 | The specified user ID is not found.            |
292| 17700025 | The specified type is invalid.                 |
293| 17700028 | The specified ability does not match the type. |
294
295**Example**
296
297```ts
298import defaultAppMgr from '@ohos.bundle.defaultAppManager';
299defaultAppMgr.setDefaultApplication(defaultAppMgr.ApplicationType.BROWSER, {
300    bundleName: "com.example.myapplication",
301    moduleName: "module01",
302    abilityName: "EntryAbility"
303}).then((data) => {
304    console.info('Operation successful.');
305}).catch((error) => {
306    console.error('Operation failed. Cause: ' + JSON.stringify(error));
307});
308
309let userId = 100;
310defaultAppMgr.setDefaultApplication(defaultAppMgr.ApplicationType.BROWSER, {
311    bundleName: "com.example.myapplication",
312    moduleName: "module01",
313    abilityName: "EntryAbility"
314}, userId).then((data) => {
315    console.info('Operation successful.');
316}).catch((error) => {
317    console.error('Operation failed. Cause: ' + JSON.stringify(error));
318});
319
320defaultAppMgr.setDefaultApplication("image/png", {
321    bundleName: "com.example.myapplication",
322    moduleName: "module01",
323    abilityName: "EntryAbility"
324}, userId).then((data) => {
325    console.info('Operation successful.');
326}).catch((error) => {
327    console.error('Operation failed. Cause: ' + JSON.stringify(error));
328});
329```
330
331## defaultAppMgr.setDefaultApplication
332
333setDefaultApplication(type: string, elementName: ElementName, userId: number, callback: AsyncCallback\<void>) : void;
334
335Sets the default application for a user based on a system-defined application type or a file type that complies with the media type format (either specified by **type** or **subtype**). This API uses an asynchronous callback to return the result.
336
337**Required permissions**: ohos.permission.SET_DEFAULT_APPLICATION
338
339**System capability**: SystemCapability.BundleManager.BundleFramework.DefaultApp
340
341**System API**: This is a system API and cannot be called by third-party applications.
342
343**Parameters**
344
345| Name        | Type    | Mandatory  | Description                                     |
346| ----------- | ------ | ---- | --------------------------------------- |
347| type  | string | Yes   | Type of the target application. It must be set to a value defined by [ApplicationType](#defaultappmgrapplicationtype) or a file type that complies with the media type format.      |
348| elementName  | [ElementName](js-apis-bundle-ElementName.md) | Yes   | Information about the element to be set as the default application.                          |
349| userId  | number | Yes   | User ID.                          |
350| callback    | AsyncCallback\<void> | Yes   | Callback used to return the result.                   |
351
352**Error codes**
353
354For details about the error codes, see [Bundle Error Codes](../errorcodes/errorcode-bundle.md).
355
356| ID| Error Message                                      |
357| -------- | ---------------------------------------------- |
358| 17700004 | The specified user ID is not found.            |
359| 17700025 | The specified type is invalid.                 |
360| 17700028 | The specified ability does not match the type. |
361
362**Example**
363
364```ts
365import defaultAppMgr from '@ohos.bundle.defaultAppManager';
366let userId = 100;
367defaultAppMgr.setDefaultApplication(defaultAppMgr.ApplicationType.BROWSER, {
368    bundleName: "com.example.myapplication",
369    moduleName: "module01",
370    abilityName: "EntryAbility"
371}, userId, (err, data) => {
372    if (err) {
373        console.error('Operation failed. Cause: ' + JSON.stringify(err));
374        return;
375    }
376    console.info('Operation successful.');
377 });
378
379defaultAppMgr.setDefaultApplication("image/png", {
380    bundleName: "com.example.myapplication",
381    moduleName: "module01",
382    abilityName: "EntryAbility"
383}, userId, (err, data) => {
384    if (err) {
385        console.error('Operation failed. Cause: ' + JSON.stringify(err));
386        return;
387    }
388    console.info('Operation successful.');
389 });
390```
391
392## defaultAppMgr.setDefaultApplication
393
394setDefaultApplication(type: string, elementName: ElementName, callback: AsyncCallback\<void>) : void;
395
396Sets the default application based on a system-defined application type or a file type that complies with the media type format (either specified by **type** or **subtype**). This API uses an asynchronous callback to return the result.
397
398**Required permissions**: ohos.permission.SET_DEFAULT_APPLICATION
399
400**System capability**: SystemCapability.BundleManager.BundleFramework.DefaultApp
401
402**System API**: This is a system API and cannot be called by third-party applications.
403
404**Parameters**
405
406| Name        | Type    | Mandatory  | Description                                     |
407| ----------- | ------ | ---- | --------------------------------------- |
408| type  | string | Yes   | Type of the target application. It must be set to a value defined by [ApplicationType](#defaultappmgrapplicationtype) or a file type that complies with the media type format.      |
409| elementName  | [ElementName](js-apis-bundle-ElementName.md) | Yes   | Information about the element to be set as the default application.                          |
410| callback    | AsyncCallback\<void> | Yes   | Callback used to return the result.                   |
411
412**Error codes**
413
414For details about the error codes, see [Bundle Error Codes](../errorcodes/errorcode-bundle.md).
415
416| ID| Error Message                                      |
417| -------- | ---------------------------------------------- |
418| 17700004 | The specified user ID is not found.            |
419| 17700025 | The specified type is invalid.                 |
420| 17700028 | The specified ability does not match the type. |
421
422**Example**
423
424```ts
425import defaultAppMgr from '@ohos.bundle.defaultAppManager';
426defaultAppMgr.setDefaultApplication(defaultAppMgr.ApplicationType.BROWSER, {
427    bundleName: "com.example.myapplication",
428    moduleName: "module01",
429    abilityName: "EntryAbility"
430}, (err, data) => {
431    if (err) {
432        console.error('Operation failed. Cause: ' + JSON.stringify(err));
433        return;
434    }
435    console.info('Operation successful.');
436 });
437
438defaultAppMgr.setDefaultApplication("image/png", {
439    bundleName: "com.example.myapplication",
440    moduleName: "module01",
441    abilityName: "EntryAbility"
442}, (err, data) => {
443    if (err) {
444        console.error('Operation failed. Cause: ' + JSON.stringify(err));
445        return;
446    }
447    console.info('Operation successful.');
448 });
449```
450
451## defaultAppMgr.resetDefaultApplication
452
453resetDefaultApplication(type: string, userId?: number): Promise\<void>
454
455Resets the default application based on a system-defined application type or a file type that complies with the media type format (either specified by **type** or **subtype**). This API uses a promise to return the result.
456
457**Required permissions**: ohos.permission.SET_DEFAULT_APPLICATION
458
459**System capability**: SystemCapability.BundleManager.BundleFramework.DefaultApp
460
461**System API**: This is a system API and cannot be called by third-party applications.
462
463**Parameters**
464
465| Name        | Type    | Mandatory  | Description                                     |
466| ----------- | ------ | ---- | --------------------------------------- |
467| type  | string | Yes   | Type of the target application. It must be set to a value defined by [ApplicationType](#defaultappmgrapplicationtype) or a file type that complies with the media type format.      |
468| userId  | number | No   | User ID. The default value is the user ID of the caller.                           |
469
470**Error codes**
471
472For details about the error codes, see [Bundle Error Codes](../errorcodes/errorcode-bundle.md).
473
474| ID| Error Message                           |
475| -------- | ----------------------------------- |
476| 17700004 | The specified user ID is not found. |
477| 17700025 | The specified type is invalid.      |
478
479**Example**
480
481```ts
482import defaultAppMgr from '@ohos.bundle.defaultAppManager';
483let userId = 100;
484defaultAppMgr.resetDefaultApplication(defaultAppMgr.ApplicationType.BROWSER, userId)
485.then((data) => {
486    console.info('Operation successful.');
487})
488.catch((error) => {
489    console.error('Operation failed. Cause: ' + JSON.stringify(error));
490});
491
492defaultAppMgr.resetDefaultApplication("image/png", userId)
493.then((data) => {
494    console.info('Operation successful.');
495})
496.catch((error) => {
497    console.error('Operation failed. Cause: ' + JSON.stringify(error));
498});
499```
500
501## defaultAppMgr.resetDefaultApplication
502
503resetDefaultApplication(type: string, userId: number, callback: AsyncCallback\<void>) : void;
504
505Resets the default application for a user based on a system-defined application type or a file type that complies with the media type format (either specified by **type** or **subtype**). This API uses an asynchronous callback to return the result.
506
507**Required permissions**: ohos.permission.SET_DEFAULT_APPLICATION
508
509**System capability**: SystemCapability.BundleManager.BundleFramework.DefaultApp
510
511**System API**: This is a system API and cannot be called by third-party applications.
512
513**Parameters**
514
515| Name        | Type    | Mandatory  | Description                                     |
516| ----------- | ------ | ---- | --------------------------------------- |
517| type  | string | Yes   | Type of the target application. It must be set to a value defined by [ApplicationType](#defaultappmgrapplicationtype) or a file type that complies with the media type format.      |
518| userId  | number | Yes   | User ID.                         |
519| callback    | AsyncCallback\<void> | Yes   | Callback used to return the result.                   |
520
521**Error codes**
522
523For details about the error codes, see [Bundle Error Codes](../errorcodes/errorcode-bundle.md).
524
525| ID| Error Message                           |
526| -------- | ----------------------------------- |
527| 17700004 | The specified user ID is not found. |
528| 17700025 | The specified type is invalid.      |
529
530**Example**
531
532```ts
533import defaultAppMgr from '@ohos.bundle.defaultAppManager';
534let userId = 100;
535defaultAppMgr.resetDefaultApplication(defaultAppMgr.ApplicationType.BROWSER, userId, (err, data) => {
536    if (err) {
537        console.error('Operation failed. Cause: ' + JSON.stringify(err));
538        return;
539    }
540    console.info('Operation successful.');
541});
542
543defaultAppMgr.resetDefaultApplication("image/png", userId, (err, data) => {
544    if (err) {
545        console.error('Operation failed. Cause: ' + JSON.stringify(err));
546        return;
547    }
548    console.info('Operation successful.');
549});
550```
551
552## defaultAppMgr.resetDefaultApplication
553
554resetDefaultApplication(type: string, callback: AsyncCallback\<void>) : void;
555
556Resets the default application based on a system-defined application type or a file type that complies with the media type format (either specified by **type** or **subtype**). This API uses an asynchronous callback to return the result.
557
558**Required permissions**: ohos.permission.SET_DEFAULT_APPLICATION
559
560**System capability**: SystemCapability.BundleManager.BundleFramework.DefaultApp
561
562**System API**: This is a system API and cannot be called by third-party applications.
563
564**Parameters**
565
566| Name        | Type    | Mandatory  | Description                                     |
567| ----------- | ------ | ---- | --------------------------------------- |
568| type  | string | Yes   | Type of the target application. It must be set to a value defined by [ApplicationType](#defaultappmgrapplicationtype) or a file type that complies with the media type format.      |
569| callback    | AsyncCallback\<void> | Yes   | Callback used to return the result.                   |
570
571**Error codes**
572
573For details about the error codes, see [Bundle Error Codes](../errorcodes/errorcode-bundle.md).
574
575| ID| Error Message                           |
576| -------- | ----------------------------------- |
577| 17700004 | The specified user ID is not found. |
578| 17700025 | The specified type is invalid.      |
579
580**Example**
581
582```ts
583import defaultAppMgr from '@ohos.bundle.defaultAppManager';
584defaultAppMgr.resetDefaultApplication(defaultAppMgr.ApplicationType.BROWSER, (err, data) => {
585    if (err) {
586        console.error('Operation failed. Cause: ' + JSON.stringify(err));
587        return;
588    }
589    console.info('Operation successful.');
590});
591
592defaultAppMgr.resetDefaultApplication("image/png", (err, data) => {
593    if (err) {
594        console.error('Operation failed. Cause: ' + JSON.stringify(err));
595        return;
596    }
597    console.info('Operation successful.');
598});
599```
600