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| 17700023 | The specified default app does not exist. | 236| 17700025 | The specified type is invalid. | 237 238**Example** 239 240```ts 241import defaultAppMgr from '@ohos.bundle.defaultAppManager'; 242defaultAppMgr.getDefaultApplication(defaultAppMgr.ApplicationType.BROWSER, (err, data) => { 243 if (err) { 244 console.error('Operation failed. Cause: ' + JSON.stringify(err)); 245 return; 246 } 247 console.info('Operation successful. bundleInfo:' + JSON.stringify(data)); 248}); 249defaultAppMgr.getDefaultApplication("image/png", (err, data) => { 250 if (err) { 251 console.error('Operation failed. Cause: ' + JSON.stringify(err)); 252 return; 253 } 254 console.info('Operation successful. bundleInfo:' + JSON.stringify(data)); 255}); 256``` 257 258## defaultAppMgr.setDefaultApplication 259 260setDefaultApplication(type: string, elementName: ElementName, userId?: number): Promise\<void> 261 262Sets 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. 263 264**Required permissions**: ohos.permission.SET_DEFAULT_APPLICATION 265 266**System capability**: SystemCapability.BundleManager.BundleFramework.DefaultApp 267 268**System API**: This is a system API and cannot be called by third-party applications. 269 270**Parameters** 271 272| Name | Type | Mandatory | Description | 273| ----------- | ------ | ---- | --------------------------------------- | 274| 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. | 275| elementName | [ElementName](js-apis-bundle-ElementName.md) | Yes | Information about the element to be set as the default application. | 276| userId | number | No | User ID. The default value is the user ID of the caller. | 277 278**Return value** 279 280| Type | Description | 281| -------------- | ---------------------------------- | 282| Promise\<void> | Promise that returns no value.| 283 284**Error codes** 285 286For details about the error codes, see [Bundle Error Codes](../errorcodes/errorcode-bundle.md). 287 288| ID| Error Message | 289| -------- | ---------------------------------------------- | 290| 17700004 | The specified user ID is not found. | 291| 17700025 | The specified type is invalid. | 292| 17700028 | The specified ability does not match the type. | 293 294**Example** 295 296```ts 297import defaultAppMgr from '@ohos.bundle.defaultAppManager'; 298defaultAppMgr.setDefaultApplication(defaultAppMgr.ApplicationType.BROWSER, { 299 bundleName: "com.test.app", 300 moduleName: "module01", 301 abilityName: "MainAbility" 302}).then((data) => { 303 console.info('Operation successful.'); 304}).catch((error) => { 305 console.error('Operation failed. Cause: ' + JSON.stringify(error)); 306}); 307 308let userId = 100; 309defaultAppMgr.setDefaultApplication(defaultAppMgr.ApplicationType.BROWSER, { 310 bundleName: "com.test.app", 311 moduleName: "module01", 312 abilityName: "MainAbility" 313}, userId).then((data) => { 314 console.info('Operation successful.'); 315}).catch((error) => { 316 console.error('Operation failed. Cause: ' + JSON.stringify(error)); 317}); 318 319defaultAppMgr.setDefaultApplication("image/png", { 320 bundleName: "com.test.app", 321 moduleName: "module01", 322 abilityName: "MainAbility" 323}, userId).then((data) => { 324 console.info('Operation successful.'); 325}).catch((error) => { 326 console.error('Operation failed. Cause: ' + JSON.stringify(error)); 327}); 328``` 329 330## defaultAppMgr.setDefaultApplication 331 332setDefaultApplication(type: string, elementName: ElementName, userId: number, callback: AsyncCallback\<void>) : void; 333 334Sets 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. 335 336**Required permissions**: ohos.permission.SET_DEFAULT_APPLICATION 337 338**System capability**: SystemCapability.BundleManager.BundleFramework.DefaultApp 339 340**System API**: This is a system API and cannot be called by third-party applications. 341 342**Parameters** 343 344| Name | Type | Mandatory | Description | 345| ----------- | ------ | ---- | --------------------------------------- | 346| 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. | 347| elementName | [ElementName](js-apis-bundle-ElementName.md) | Yes | Information about the element to be set as the default application. | 348| userId | number | Yes | User ID. | 349| callback | AsyncCallback\<void> | Yes | Callback used to return the result. | 350 351**Error codes** 352 353For details about the error codes, see [Bundle Error Codes](../errorcodes/errorcode-bundle.md). 354 355| ID| Error Message | 356| -------- | ---------------------------------------------- | 357| 17700004 | The specified user ID is not found. | 358| 17700025 | The specified type is invalid. | 359| 17700028 | The specified ability does not match the type. | 360 361**Example** 362 363```ts 364import defaultAppMgr from '@ohos.bundle.defaultAppManager'; 365let userId = 100; 366defaultAppMgr.setDefaultApplication(defaultAppMgr.ApplicationType.BROWSER, { 367 bundleName: "com.test.app", 368 moduleName: "module01", 369 abilityName: "MainAbility" 370}, userId, (err, data) => { 371 if (err) { 372 console.error('Operation failed. Cause: ' + JSON.stringify(err)); 373 return; 374 } 375 console.info('Operation successful.'); 376 }); 377 378defaultAppMgr.setDefaultApplication("image/png", { 379 bundleName: "com.test.app", 380 moduleName: "module01", 381 abilityName: "MainAbility" 382}, userId, (err, data) => { 383 if (err) { 384 console.error('Operation failed. Cause: ' + JSON.stringify(err)); 385 return; 386 } 387 console.info('Operation successful.'); 388 }); 389``` 390 391## defaultAppMgr.setDefaultApplication 392 393setDefaultApplication(type: string, elementName: ElementName, callback: AsyncCallback\<void>) : void; 394 395Sets 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. 396 397**Required permissions**: ohos.permission.SET_DEFAULT_APPLICATION 398 399**System capability**: SystemCapability.BundleManager.BundleFramework.DefaultApp 400 401**System API**: This is a system API and cannot be called by third-party applications. 402 403**Parameters** 404 405| Name | Type | Mandatory | Description | 406| ----------- | ------ | ---- | --------------------------------------- | 407| 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. | 408| elementName | [ElementName](js-apis-bundle-ElementName.md) | Yes | Information about the element to be set as the default application. | 409| callback | AsyncCallback\<void> | Yes | Callback used to return the result. | 410 411**Error codes** 412 413For details about the error codes, see [Bundle Error Codes](../errorcodes/errorcode-bundle.md). 414 415| ID| Error Message | 416| -------- | ---------------------------------------------- | 417| 17700025 | The specified type is invalid. | 418| 17700028 | The specified ability does not match the type. | 419 420**Example** 421 422```ts 423import defaultAppMgr from '@ohos.bundle.defaultAppManager'; 424defaultAppMgr.setDefaultApplication(defaultAppMgr.ApplicationType.BROWSER, { 425 bundleName: "com.test.app", 426 moduleName: "module01", 427 abilityName: "MainAbility" 428}, (err, data) => { 429 if (err) { 430 console.error('Operation failed. Cause: ' + JSON.stringify(err)); 431 return; 432 } 433 console.info('Operation successful.'); 434 }); 435 436defaultAppMgr.setDefaultApplication("image/png", { 437 bundleName: "com.test.app", 438 moduleName: "module01", 439 abilityName: "MainAbility" 440}, (err, data) => { 441 if (err) { 442 console.error('Operation failed. Cause: ' + JSON.stringify(err)); 443 return; 444 } 445 console.info('Operation successful.'); 446 }); 447``` 448 449## defaultAppMgr.resetDefaultApplication 450 451resetDefaultApplication(type: string, userId?: number): Promise\<void> 452 453Resets 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. 454 455**Required permissions**: ohos.permission.SET_DEFAULT_APPLICATION 456 457**System capability**: SystemCapability.BundleManager.BundleFramework.DefaultApp 458 459**System API**: This is a system API and cannot be called by third-party applications. 460 461**Parameters** 462 463| Name | Type | Mandatory | Description | 464| ----------- | ------ | ---- | --------------------------------------- | 465| 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. | 466| userId | number | No | User ID. The default value is the user ID of the caller. | 467 468**Error codes** 469 470For details about the error codes, see [Bundle Error Codes](../errorcodes/errorcode-bundle.md). 471 472| ID| Error Message | 473| -------- | ----------------------------------- | 474| 17700004 | The specified user ID is not found. | 475| 17700025 | The specified type is invalid. | 476 477**Example** 478 479```ts 480import defaultAppMgr from '@ohos.bundle.defaultAppManager'; 481let userId = 100; 482defaultAppMgr.resetDefaultApplication(defaultAppMgr.ApplicationType.BROWSER, userId) 483.then((data) => { 484 console.info('Operation successful.'); 485}) 486.catch((error) => { 487 console.error('Operation failed. Cause: ' + JSON.stringify(error)); 488}); 489 490defaultAppMgr.resetDefaultApplication("image/png", userId) 491.then((data) => { 492 console.info('Operation successful.'); 493}) 494.catch((error) => { 495 console.error('Operation failed. Cause: ' + JSON.stringify(error)); 496}); 497``` 498 499## defaultAppMgr.resetDefaultApplication 500 501resetDefaultApplication(type: string, userId: number, callback: AsyncCallback\<void>) : void; 502 503Resets 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. 504 505**Required permissions**: ohos.permission.SET_DEFAULT_APPLICATION 506 507**System capability**: SystemCapability.BundleManager.BundleFramework.DefaultApp 508 509**System API**: This is a system API and cannot be called by third-party applications. 510 511**Parameters** 512 513| Name | Type | Mandatory | Description | 514| ----------- | ------ | ---- | --------------------------------------- | 515| 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. | 516| userId | number | Yes | User ID. | 517| callback | AsyncCallback\<void> | Yes | Callback used to return the result. | 518 519**Error codes** 520 521For details about the error codes, see [Bundle Error Codes](../errorcodes/errorcode-bundle.md). 522 523| ID| Error Message | 524| -------- | ----------------------------------- | 525| 17700004 | The specified user ID is not found. | 526| 17700025 | The specified type is invalid. | 527 528**Example** 529 530```ts 531import defaultAppMgr from '@ohos.bundle.defaultAppManager'; 532let userId = 100; 533defaultAppMgr.resetDefaultApplication(defaultAppMgr.ApplicationType.BROWSER, userId, (err, data) => { 534 if (err) { 535 console.error('Operation failed. Cause: ' + JSON.stringify(err)); 536 return; 537 } 538 console.info('Operation successful.'); 539}); 540 541defaultAppMgr.resetDefaultApplication("image/png", userId, (err, data) => { 542 if (err) { 543 console.error('Operation failed. Cause: ' + JSON.stringify(err)); 544 return; 545 } 546 console.info('Operation successful.'); 547}); 548``` 549 550## defaultAppMgr.resetDefaultApplication 551 552resetDefaultApplication(type: string, callback: AsyncCallback\<void>) : void; 553 554Resets 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. 555 556**Required permissions**: ohos.permission.SET_DEFAULT_APPLICATION 557 558**System capability**: SystemCapability.BundleManager.BundleFramework.DefaultApp 559 560**System API**: This is a system API and cannot be called by third-party applications. 561 562**Parameters** 563 564| Name | Type | Mandatory | Description | 565| ----------- | ------ | ---- | --------------------------------------- | 566| 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. | 567| callback | AsyncCallback\<void> | Yes | Callback used to return the result. | 568 569**Error codes** 570 571For details about the error codes, see [Bundle Error Codes](../errorcodes/errorcode-bundle.md). 572 573| ID| Error Message | 574| -------- | ----------------------------------- | 575| 17700025 | The specified type is invalid. | 576 577**Example** 578 579```ts 580import defaultAppMgr from '@ohos.bundle.defaultAppManager'; 581defaultAppMgr.resetDefaultApplication(defaultAppMgr.ApplicationType.BROWSER, (err, data) => { 582 if (err) { 583 console.error('Operation failed. Cause: ' + JSON.stringify(err)); 584 return; 585 } 586 console.info('Operation successful.'); 587}); 588 589defaultAppMgr.resetDefaultApplication("image/png", (err, data) => { 590 if (err) { 591 console.error('Operation failed. Cause: ' + JSON.stringify(err)); 592 return; 593 } 594 console.info('Operation successful.'); 595}); 596``` 597