• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# @ohos.app.ability.abilityManager (AbilityManager) (System API)
2
3The **AbilityManager** module provides APIs for obtaining, adding, and updating ability running information and state information.
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> The APIs of this module are system APIs and cannot be called by third-party applications.
9
10## Modules to Import
11
12```ts
13import abilityManager from '@ohos.app.ability.abilityManager';
14```
15
16## AbilityState
17
18Enumerates the ability states. This enum can be used together with [AbilityRunningInfo](js-apis-inner-application-abilityRunningInfo-sys.md) to return the ability state.
19
20**System API**: This is a system API.
21
22**System capability**: SystemCapability.Ability.AbilityRuntime.Core
23
24| Name| Value| Description|
25| -------- | -------- | -------- |
26| INITIAL | 0 | The ability is in the initial state.|
27| FOCUS | 2 | The ability has the focus.|
28| FOREGROUND | 9 | The ability is in the foreground state. |
29| BACKGROUND | 10 | The ability is in the background state. |
30| FOREGROUNDING | 11 | The ability is in the state of being switched to the foreground. |
31| BACKGROUNDING | 12 | The ability is in the state of being switched to the background. |
32
33## updateConfiguration
34
35updateConfiguration(config: Configuration, callback: AsyncCallback\<void>): void
36
37Updates the configuration. This API uses an asynchronous callback to return the result.
38
39**System API**: This is a system API.
40
41**Permission required**: ohos.permission.UPDATE_CONFIGURATION
42
43**System capability**: SystemCapability.Ability.AbilityRuntime.Core
44
45**Parameters**
46
47| Name       | Type                                      | Mandatory  | Description            |
48| --------- | ---------------------------------------- | ---- | -------------- |
49| config    | [Configuration](js-apis-app-ability-configuration.md)   | Yes   | New configuration. You only need to configure the items to be updated.|
50| callback  | AsyncCallback\<void>                   | Yes   | Callback used to return the API call result. You can perform error handling or custom processing in it.     |
51
52**Error codes**
53
54| ID| Error Message|
55| ------- | -------- |
56| 16000050 | Internal error. |
57
58For details about the error codes, see [Ability Error Codes](errorcode-ability.md).
59
60**Example**
61
62```ts
63import abilityManager from '@ohos.app.ability.abilityManager';
64import { Configuration } from '@ohos.app.ability.Configuration';
65import ConfigurationConstant from '@ohos.app.ability.ConfigurationConstant';
66import { BusinessError } from '@ohos.base';
67
68const config: Configuration = {
69  language: 'Zh-Hans',                 // Simplified Chinese.
70  colorMode: ConfigurationConstant.ColorMode.COLOR_MODE_LIGHT,         // Light theme.
71  direction: ConfigurationConstant.Direction.DIRECTION_VERTICAL,       // Vertical direction.
72  screenDensity: ConfigurationConstant.ScreenDensity.SCREEN_DENSITY_SDPI,  // The screen pixel density is 'sdpi'.
73  displayId: 1,                        // The application is displayed on the display with ID 1.
74  hasPointerDevice: true,              // A pointer device is connected.
75};
76
77try {
78    abilityManager.updateConfiguration(config, (err: BusinessError) => {
79        if (err) {
80            console.error(`updateConfiguration fail, err: ${JSON.stringify(err)}`);
81        } else {
82            console.log('updateConfiguration success.');
83        }
84    });
85} catch (paramError) {
86    let code: number = (paramError as BusinessError).code;
87    let message: string = (paramError as BusinessError).message;
88    console.error(`error.code: ${code}, error.message: ${message}`);
89}
90```
91
92## updateConfiguration
93
94updateConfiguration(config: Configuration): Promise\<void>
95
96Updates the configuration. This API uses a promise to return the result.
97
98**System API**: This is a system API.
99
100**Permission required**: ohos.permission.UPDATE_CONFIGURATION
101
102**System capability**: SystemCapability.Ability.AbilityRuntime.Core
103
104**Parameters**
105
106| Name       | Type                                      | Mandatory  | Description            |
107| --------- | ---------------------------------------- | ---- | -------------- |
108| config    | [Configuration](js-apis-app-ability-configuration.md)   | Yes   | New configuration. You only need to configure the items to be updated.|
109
110**Return value**
111
112| Type                                      | Description     |
113| ---------------------------------------- | ------- |
114| Promise\<void> | Promise used to return the API call result. You can perform error handling or custom processing in it.|
115
116**Error codes**
117
118| ID| Error Message|
119| ------- | -------- |
120| 16000050 | Internal error. |
121
122For details about the error codes, see [Ability Error Codes](errorcode-ability.md).
123
124**Example**
125
126```ts
127import abilityManager from '@ohos.app.ability.abilityManager';
128import { Configuration } from '@ohos.app.ability.Configuration';
129import ConfigurationConstant from '@ohos.app.ability.ConfigurationConstant';
130import { BusinessError } from '@ohos.base';
131
132const config: Configuration = {
133  language: 'Zh-Hans',                 // Simplified Chinese.
134  colorMode: ConfigurationConstant.ColorMode.COLOR_MODE_LIGHT,         // Light theme.
135  direction: ConfigurationConstant.Direction.DIRECTION_VERTICAL,       // Vertical direction.
136  screenDensity: ConfigurationConstant.ScreenDensity.SCREEN_DENSITY_SDPI,  // The screen pixel density is 'sdpi'.
137  displayId: 1,                        // The application is displayed on the display with ID 1.
138  hasPointerDevice: true,              // A pointer device is connected.
139};
140
141try {
142    abilityManager.updateConfiguration(config).then(() => {
143        console.log('updateConfiguration success.');
144    }).catch((err: BusinessError) => {
145        console.error(`updateConfiguration fail, err: ${JSON.stringify(err)}`);
146    });
147} catch (paramError) {
148    let code: number = (paramError as BusinessError).code;
149    let message: string = (paramError as BusinessError).message;
150    console.error(`error.code: ${code}, error.message: ${message}`);
151}
152```
153
154## getAbilityRunningInfos
155
156getAbilityRunningInfos(callback: AsyncCallback\<Array\<AbilityRunningInfo>>): void
157
158Obtains the UIAbility running information. This API uses an asynchronous callback to return the result.
159
160**System API**: This is a system API.
161
162**Required permissions**: ohos.permission.GET_RUNNING_INFO
163
164**System capability**: SystemCapability.Ability.AbilityRuntime.Core
165
166**Parameters**
167
168| Name       | Type                                      | Mandatory  | Description            |
169| --------- | ---------------------------------------- | ---- | -------------- |
170| callback  | AsyncCallback\<Array\<[AbilityRunningInfo](js-apis-inner-application-abilityRunningInfo-sys.md)>>  | Yes   | Callback used to return the API call result and the ability running information. You can perform error handling or custom processing in it.     |
171
172**Error codes**
173
174| ID| Error Message|
175| ------- | -------- |
176| 16000050 | Internal error. |
177
178For details about the error codes, see [Ability Error Codes](errorcode-ability.md).
179
180**Example**
181
182```ts
183import abilityManager from '@ohos.app.ability.abilityManager';
184import { BusinessError } from '@ohos.base';
185
186try {
187    abilityManager.getAbilityRunningInfos((err: BusinessError, data: Array<abilityManager.AbilityRunningInfo>) => {
188        if (err) {
189            console.error(`getAbilityRunningInfos fail, error: ${JSON.stringify(err)}`);
190        } else {
191            console.log(`getAbilityRunningInfos success, data: ${JSON.stringify(data)}`);
192        }
193    });
194} catch (paramError) {
195    let code: number = (paramError as BusinessError).code;
196    let message: string = (paramError as BusinessError).message;
197    console.error(`error.code: ${code}, error.message: ${message}`);
198}
199```
200
201## getAbilityRunningInfos
202
203getAbilityRunningInfos(): Promise\<Array\<AbilityRunningInfo>>
204
205Obtains the UIAbility running information. This API uses a promise to return the result.
206
207**System API**: This is a system API.
208
209**Required permissions**: ohos.permission.GET_RUNNING_INFO
210
211**System capability**: SystemCapability.Ability.AbilityRuntime.Core
212
213**Return value**
214
215| Type                                      | Description     |
216| ---------------------------------------- | ------- |
217| Promise\<Array\<[AbilityRunningInfo](js-apis-inner-application-abilityRunningInfo-sys.md)>> | Promise used to return the API call result and the UIAbility running information. You can perform error handling or custom processing in it.|
218
219**Error codes**
220
221| ID| Error Message|
222| ------- | -------- |
223| 16000050 | Internal error. |
224
225For details about the error codes, see [Ability Error Codes](errorcode-ability.md).
226
227**Example**
228
229```ts
230import abilityManager from '@ohos.app.ability.abilityManager';
231import { BusinessError } from '@ohos.base';
232
233try {
234    abilityManager.getAbilityRunningInfos().then((data: Array<abilityManager.AbilityRunningInfo>) => {
235        console.log(`getAbilityRunningInfos success, data: ${JSON.stringify(data)}`);
236    }).catch((err: BusinessError) => {
237        console.error(`getAbilityRunningInfos fail, err: ${JSON.stringify(err)}`);
238    });
239} catch (paramError) {
240    let code: number = (paramError as BusinessError).code;
241    let message: string = (paramError as BusinessError).message;
242    console.error(`error.code: ${code}, error.message: ${message}`);
243}
244```
245
246## getExtensionRunningInfos
247
248getExtensionRunningInfos(upperLimit: number, callback: AsyncCallback\<Array\<ExtensionRunningInfo>>): void
249
250Obtains the ExtensionAbility running information. This API uses an asynchronous callback to return the result.
251
252**System API**: This is a system API.
253
254**Required permissions**: ohos.permission.GET_RUNNING_INFO
255
256**System capability**: SystemCapability.Ability.AbilityRuntime.Core
257
258**Parameters**
259
260| Name       | Type                                      | Mandatory  | Description            |
261| --------- | ---------------------------------------- | ---- | -------------- |
262| upperLimit | number                                   | Yes| Maximum number of messages that can be obtained. The maximum value is 2<sup>31</sup>-1.|
263| callback  | AsyncCallback\<Array\<[ExtensionRunningInfo](js-apis-inner-application-extensionRunningInfo-sys.md)>>  | Yes   | Callback used to return the API call result and the ExtensionAbility running information. You can perform error handling or custom processing in it.     |
264
265**Error codes**
266
267| ID| Error Message|
268| ------- | -------- |
269| 16000050 | Internal error. |
270
271For details about the error codes, see [Ability Error Codes](errorcode-ability.md).
272
273**Example**
274
275```ts
276import abilityManager from '@ohos.app.ability.abilityManager';
277import { BusinessError } from '@ohos.base';
278
279let upperLimit = 10;
280
281try {
282    abilityManager.getExtensionRunningInfos(upperLimit, (err: BusinessError, data: Array<abilityManager.ExtensionRunningInfo>) => {
283        if (err) {
284            console.error(`getExtensionRunningInfos fail, err: ${JSON.stringify(err)}`);
285        } else {
286            console.log(`getExtensionRunningInfos success, data: ${JSON.stringify(data)}`);
287        }
288    });
289} catch (paramError) {
290    let code: number = (paramError as BusinessError).code;
291    let message: string = (paramError as BusinessError).message;
292    console.error(`error.code: ${code}, error.message: ${message}`);
293}
294```
295
296## getExtensionRunningInfos
297
298getExtensionRunningInfos(upperLimit: number): Promise\<Array\<ExtensionRunningInfo>>
299
300Obtains the ExtensionAbility running information. This API uses a promise to return the result.
301
302**System API**: This is a system API.
303
304**Required permissions**: ohos.permission.GET_RUNNING_INFO
305
306**System capability**: SystemCapability.Ability.AbilityRuntime.Core
307
308**Parameters**
309
310| Name       | Type                                      | Mandatory  | Description            |
311| --------- | ---------------------------------------- | ---- | -------------- |
312| upperLimit | number                                   | Yes| Maximum number of messages that can be obtained. The maximum value is 2<sup>31</sup>-1.|
313
314**Return value**
315
316| Type                                      | Description     |
317| ---------------------------------------- | ------- |
318| Promise\<Array\<[ExtensionRunningInfo](js-apis-inner-application-extensionRunningInfo-sys.md)>> | Promise used to return the API call result and the ExtensionAbility running information. You can perform error handling or custom processing in it.|
319
320**Error codes**
321
322| ID| Error Message|
323| ------- | -------- |
324| 16000050 | Internal error. |
325
326For details about the error codes, see [Ability Error Codes](errorcode-ability.md).
327
328**Example**
329
330```ts
331import abilityManager from '@ohos.app.ability.abilityManager';
332import { BusinessError } from '@ohos.base';
333
334let upperLimit = 10;
335
336try {
337    abilityManager.getExtensionRunningInfos(upperLimit).then((data: Array<abilityManager.ExtensionRunningInfo>) => {
338        console.log(`getExtensionRunningInfos success, data: ${JSON.stringify(data)}`);
339    }).catch((err: BusinessError) => {
340        console.error(`getExtensionRunningInfos fail, err: ${JSON.stringify(err)}`);
341    });
342} catch (paramError) {
343    let code: number = (paramError as BusinessError).code;
344    let message: string = (paramError as BusinessError).message;
345    console.error(`error.code: ${code}, error.message: ${message}`);
346}
347```
348
349## getTopAbility
350
351getTopAbility(callback: AsyncCallback\<ElementName>): void
352
353Obtains the top ability, which is the ability that has the window focus. This API uses an asynchronous callback to return the result.
354
355**System API**: This is a system API.
356
357**System capability**: SystemCapability.Ability.AbilityRuntime.Core
358
359**Parameters**
360
361| Name       | Type                                      | Mandatory  | Description            |
362| --------- | ---------------------------------------- | ---- | -------------- |
363| callback  | AsyncCallback\<[ElementName](js-apis-bundleManager-elementName.md)>  | Yes   | Callback used to return the API call result and the element name of the top ability. You can perform error handling or custom processing in it.     |
364
365**Error codes**
366
367| ID| Error Message|
368| ------- | -------- |
369| 16000050 | Internal error. |
370
371For details about the error codes, see [Ability Error Codes](errorcode-ability.md).
372
373**Example**
374
375```ts
376import abilityManager from '@ohos.app.ability.abilityManager';
377import { BusinessError } from '@ohos.base';
378
379abilityManager.getTopAbility((err: BusinessError, data) => {
380    if (err) {
381        console.error(`getTopAbility fail, err: ${JSON.stringify(err)}`);
382    } else {
383        console.log(`getTopAbility success, data: ${JSON.stringify(data)}`);
384    }
385});
386```
387
388## getTopAbility
389
390getTopAbility(): Promise\<ElementName>
391
392Obtains the top ability, which is the ability that has the window focus. This API uses a promise to return the result.
393
394**System API**: This is a system API.
395
396**System capability**: SystemCapability.Ability.AbilityRuntime.Core
397
398**Return value**
399
400| Type                                      | Description     |
401| ---------------------------------------- | ------- |
402| Promise\<[ElementName](js-apis-bundleManager-elementName.md)>| Promise used to return the API call result and the element name of the top ability. You can perform error handling or custom processing in it.|
403
404**Error codes**
405
406| ID| Error Message|
407| ------- | -------- |
408| 16000050 | Internal error. |
409
410For details about the error codes, see [Ability Error Codes](errorcode-ability.md).
411
412**Example**
413
414```ts
415import abilityManager from '@ohos.app.ability.abilityManager';
416import { BusinessError } from '@ohos.base';
417
418abilityManager.getTopAbility().then((data) => {
419    console.log(`getTopAbility success, data: ${JSON.stringify(data)}`);
420}).catch((err: BusinessError) => {
421    console.error(`getTopAbility fail, err: ${JSON.stringify(err)}`);
422});
423```
424
425## acquireShareData<sup>10+</sup>
426
427acquireShareData(missionId: number, callback: AsyncCallback\<Record\<string, Object>>): void
428
429Called by a system dialog box to obtain shared data, which is set by the target UIAbility through **onShare()**. This API uses an asynchronous callback to return the result.
430
431**System API**: This is a system API.
432
433**System capability**: SystemCapability.Ability.AbilityRuntime.Core
434
435**Parameters**
436
437| Name       | Type                                      | Mandatory  | Description            |
438| --------- | ---------------------------------------- | ---- | -------------- |
439| missionId | number                                   | Yes| Mission ID on the target application. The maximum value is 2<sup>31</sup>-1.|
440| callback  | AsyncCallback\<Record\<string, Object>>  | Yes   | Callback used to return the API call result and the shared data. You can perform error handling or custom processing in it.     |
441
442**Error codes**
443
444| ID| Error Message|
445| ------- | -------- |
446| 16000050 | Internal error. |
447
448For details about the error codes, see [Ability Error Codes](errorcode-ability.md).
449
450**Example**
451
452```ts
453import abilityManager from '@ohos.app.ability.abilityManager';
454import { BusinessError } from '@ohos.base';
455
456try {
457    abilityManager.acquireShareData(1, (err: BusinessError, wantParam: Record<string, Object>) => {
458        if (err) {
459            console.error(`acquireShareData fail, err: ${JSON.stringify(err)}`);
460        } else {
461            console.log(`acquireShareData success, data: ${JSON.stringify(wantParam)}`);
462        }
463    });
464} catch (paramError) {
465    let code: number = (paramError as BusinessError).code;
466    let message: string = (paramError as BusinessError).message;
467    console.error(`error.code: ${code}, error.message: ${message}`);
468}
469
470```
471
472## acquireShareData<sup>10+</sup>
473
474acquireShareData(missionId: number): Promise\<Record\<string, Object>>
475
476Called by a system dialog box to obtain shared data, which is set by the target UIAbility through **onShare()**. This API uses a promise to return the result.
477
478**System API**: This is a system API.
479
480**System capability**: SystemCapability.Ability.AbilityRuntime.Core
481
482**Parameters**
483
484| Name       | Type                                      | Mandatory  | Description            |
485| --------- | ---------------------------------------- | ---- | -------------- |
486| missionId | number                                   | Yes| Mission ID on the target application. The maximum value is 2<sup>31</sup>-1.|
487
488**Return value**
489
490| Type                                      | Description     |
491| ---------------------------------------- | ------- |
492| Promise\<Record\<string, Object>>| Promise used to return the API call result and the shared data. You can perform error handling or custom processing in it.|
493
494**Error codes**
495
496| ID| Error Message|
497| ------- | -------- |
498| 16000050 | Internal error. |
499
500For details about the error codes, see [Ability Error Codes](errorcode-ability.md).
501
502**Example**
503
504```ts
505import abilityManager from '@ohos.app.ability.abilityManager';
506import { BusinessError } from '@ohos.base';
507
508try {
509    abilityManager.acquireShareData(1).then((wantParam: Record<string, Object>) => {
510    console.log(`acquireShareData success, data: ${JSON.stringify(wantParam)}`);
511    }).catch((err: BusinessError) => {
512    console.error(`acquireShareData fail, err: ${JSON.stringify(err)}`);
513    });
514} catch (paramError) {
515    let code: number = (paramError as BusinessError).code;
516    let message: string = (paramError as BusinessError).message;
517    console.error(`error.code: ${code}, error.message: ${message}`);
518}
519```
520
521## notifySaveAsResult<sup>10+</sup>
522
523notifySaveAsResult(parameter: AbilityResult, requestCode: number, callback: AsyncCallback\<void>): void
524
525Used by the [Data Loss Prevention (DLP)](../apis-data-loss-prevention-kit/js-apis-dlppermission.md) management application to notify a sandbox application of the data saving result. This API uses an asynchronous callback to return the result.
526
527**Model restriction**: This API can be used only in the stage model.
528
529**System API**: This is a system API.
530
531**System capability**: SystemCapability.Ability.AbilityRuntime.Core
532
533**Parameters**
534
535| Name       | Type                                      | Mandatory  | Description            |
536| --------- | ---------------------------------------- | ---- | -------------- |
537| parameter | [AbilityResult](js-apis-inner-ability-abilityResult.md) | Yes| Information returned to the initiator UIAbility.|
538| requestCode | number                                        | Yes| Request code passed in by the DLP management application.         |
539| callback  | AsyncCallback<void\>                             | Yes| Callback used to return the result. If the operation is successful, **err** is **undefined**; otherwise, **err** is an error object.        |
540
541**Error codes**
542
543| ID| Error Message|
544| ------- | -------- |
545| 16000050 | Internal error. |
546
547For details about the error codes, see [Ability Error Codes](errorcode-ability.md).
548
549**Example**
550
551```ts
552import abilityManager from '@ohos.app.ability.abilityManager';
553import common from '@ohos.app.ability.common';
554import Want from '@ohos.app.ability.Want';
555import { BusinessError } from '@ohos.base';
556let want: Want = {
557  bundleName: 'com.example.myapplication',
558  abilityName: 'EntryAbility'
559};
560let resultCode = 100;
561// AbilityResult information returned to the initiator of the save-as behavior.
562let abilityResult: common.AbilityResult = {
563    want,
564    resultCode
565};
566let requestCode = 1;
567try {
568  abilityManager.notifySaveAsResult(abilityResult, requestCode, (err: BusinessError) => {
569    if (err && err.code != 0) {
570      console.error(`notifySaveAsResult fail, err: ${JSON.stringify(err)}`);
571    } else {
572      console.log(`notifySaveAsResult success`);
573    }
574  });
575} catch (paramError) {
576    let code: number = (paramError as BusinessError).code;
577    let message: string = (paramError as BusinessError).message;
578    console.error(`error.code: ${code}, error.message: ${message}`);
579}
580```
581
582## notifySaveAsResult<sup>10+</sup>
583
584notifySaveAsResult(parameter: AbilityResult, requestCode: number): Promise\<void>
585
586Used by the [Data Loss Prevention (DLP)](../apis-data-loss-prevention-kit/js-apis-dlppermission.md) management application to notify a sandbox application of the data saving result. This API uses a promise to return the result.
587
588**Model restriction**: This API can be used only in the stage model.
589
590**System API**: This is a system API.
591
592**System capability**: SystemCapability.Ability.AbilityRuntime.Core
593
594**Parameters**
595
596| Name       | Type                                      | Mandatory  | Description            |
597| --------- | ---------------------------------------- | ---- | -------------- |
598| parameter | [AbilityResult](js-apis-inner-ability-abilityResult.md) | Yes| Information returned to the initiator UIAbility.|
599| requestCode | number                                        | Yes| Request code passed in by the DLP management application.         |
600
601**Return value**
602
603| Type                                      | Description     |
604| ---------------------------------------- | ------- |
605| Promise<void\>| Promise that returns no value.|
606
607**Error codes**
608
609| ID| Error Message|
610| ------- | -------- |
611| 16000050 | Internal error. |
612
613For details about the error codes, see [Ability Error Codes](errorcode-ability.md).
614
615**Example**
616
617```ts
618import abilityManager from '@ohos.app.ability.abilityManager';
619import common from '@ohos.app.ability.common';
620import Want from '@ohos.app.ability.Want';
621import { BusinessError } from '@ohos.base';
622let want: Want = {
623  bundleName: 'com.example.myapplication',
624  abilityName: 'EntryAbility'
625};
626let resultCode = 100;
627// AbilityResult information returned to the initiator of the save-as behavior.
628let abilityResult: common.AbilityResult = {
629    want,
630    resultCode
631};
632let requestCode = 1;
633try {
634  abilityManager.notifySaveAsResult(abilityResult, requestCode).catch((err: BusinessError) => {
635    console.error(`notifySaveAsResult fail, err: ${JSON.stringify(err)}`);
636  }).then(() => {
637    console.log(`notifySaveAsResult success`);
638  });
639} catch (paramError) {
640    let code: number = (paramError as BusinessError).code;
641    let message: string = (paramError as BusinessError).message;
642    console.error(`error.code: ${code}, error.message: ${message}`);
643}
644```
645
646## abilityManager.on<sup>11+</sup>
647
648on(type: 'abilityForegroundState', observer: AbilityForegroundStateObserver): void
649
650Registers an observer to listen for ability start or exit events.
651
652**System API**: This is a system API.
653
654**Required permissions**: ohos.permission.RUNNING_STATE_OBSERVER
655
656**System capability**: SystemCapability.Ability.AbilityRuntime.Core
657
658**Parameters**
659
660| Name| Type| Mandatory| Description|
661| -------- | -------- | -------- | -------- |
662| type | string | Yes| Event type. It is fixed at **'abilityForegroundState'**.|
663| observer | [AbilityForegroundStateObserver](js-apis-inner-application-abilityForegroundStateObserver-sys) | Yes| Observer used to listen for ability start or exit events.|
664
665**Error codes**
666
667| ID| Error Message|
668| ------- | -------- |
669| 16000050 | Internal error. |
670
671For details about the error codes, see [Ability Error Codes](errorcode-ability.md).
672
673**Example**
674
675```ts
676import abilityManager from '@ohos.app.ability.abilityManager';
677import { BusinessError } from '@ohos.base';
678
679let observer: abilityManager.AbilityForegroundStateObserver = {
680    onAbilityStateChanged(abilityStateData) {
681        console.log(`onAbilityStateChanged: ${JSON.stringify(abilityStateData)}`);
682    },
683};
684try {
685    abilityManager.on('abilityForegroundState', observer);
686} catch (paramError) {
687    let code = (paramError as BusinessError).code;
688    let message = (paramError as BusinessError).message;
689    console.error(`error: ${code}, ${message} `);
690}
691```
692
693## abilityManager.off<sup>11+</sup>
694
695off(type: 'abilityForegroundState', observer?: AbilityForegroundStateObserver): void
696
697Unregisters the observer used to listen for ability start or exit events.
698
699**System API**: This is a system API.
700
701**Required permissions**: ohos.permission.RUNNING_STATE_OBSERVER
702
703**System capability**: SystemCapability.Ability.AbilityRuntime.Core
704
705**Parameters**
706
707| Name| Type| Mandatory| Description|
708| -------- | -------- | -------- | -------- |
709| type | string | Yes| Event type. It is fixed at **'abilityForegroundState'**.|
710| observer | [AbilityForegroundStateObserver](js-apis-inner-application-abilityForegroundStateObserver-sys) | No| Observer used to listen for ability start or exit events. If this parameter is not set, all observers associated with the specified event are deregistered. If this parameter is set, only the specified observer is deregistered.|
711
712**Error codes**
713
714| ID| Error Message|
715| ------- | -------- |
716| 16000050 | Internal error. |
717
718For details about the error codes, see [Ability Error Codes](errorcode-ability.md).
719
720**Example**
721
722```ts
723import abilityManager from '@ohos.app.ability.abilityManager';
724import { BusinessError } from '@ohos.base';
725let observer_: abilityManager.AbilityForegroundStateObserver | undefined;
726// 1. Register an observer to listen for ability start or exit events.
727let observer: abilityManager.AbilityForegroundStateObserver = {
728    onAbilityStateChanged(abilityStateData: abilityManager.AbilityStateData) {
729        console.log(`onAbilityStateChanged: ${JSON.stringify(abilityStateData)}`);
730    },
731};
732try {
733    abilityManager.on('abilityForegroundState', observer);
734    observer_ = observer;
735} catch (paramError) {
736    let code = (paramError as BusinessError).code;
737    let message = (paramError as BusinessError).message;
738    console.error(`error: ${code}, ${message} `);
739}
740
741// 2. Deregister the observer.
742try {
743    abilityManager.off('abilityForegroundState',  observer_);
744} catch (paramError) {
745    let code = (paramError as BusinessError).code;
746    let message = (paramError as BusinessError).message;
747    console.error(`error: ${code}, ${message} `);
748}
749```
750
751## abilityManager.getForegroundUIAbilities<sup>11+</sup>
752
753getForegroundUIAbilities(callback: AsyncCallback\<Array\<AbilityStateData>>): void
754
755Obtains the information about the UIAbilities of an application that is running in the foreground. This API uses an asynchronous callback to return the result.
756
757**System API**: This is a system API.
758
759**Required permissions**: ohos.permission.GET_RUNNING_INFO
760
761**System capability**: SystemCapability.Ability.AbilityRuntime.Core
762
763**Parameters**
764
765  | Name| Type| Mandatory| Description|
766  | -------- | -------- | -------- | -------- |
767  | callback | AsyncCallback\<Array\<[AbilityStateData](js-apis-inner-application-abilityStateData-sys.md)>>  | Yes|Callback used to return the API call result and the UIAbility information. You can perform error handling or custom processing in it.|
768
769**Error codes**
770
771| ID| Error Message|
772| ------- | -------- |
773| 16000050 | Internal error. |
774
775For details about the error codes, see [Ability Error Codes](errorcode-ability.md).
776
777**Example**
778
779```ts
780import abilityManager from '@ohos.app.ability.abilityManager';
781import { BusinessError } from '@ohos.base';
782
783abilityManager.getForegroundUIAbilities((err: BusinessError, data: Array<abilityManager.AbilityStateData>) => {
784    if (err) {
785        console.error(`Get foreground ui abilities failed, error: ${JSON.stringify(err)}`);
786    } else {
787        console.log(`Get foreground ui abilities data is: ${JSON.stringify(data)}`);
788    }
789});
790```
791
792## abilityManager.getForegroundUIAbilities<sup>11+</sup>
793
794getForegroundUIAbilities(): Promise\<Array\<AbilityStateData>>
795
796Obtains the information about the UIAbilities of an application that is running in the foreground. This API uses a promise to return the result.
797
798**System API**: This is a system API.
799
800**Required permissions**: ohos.permission.GET_RUNNING_INFO
801
802**System capability**: SystemCapability.Ability.AbilityRuntime.Core
803
804**Return value**
805
806| Type| Description|
807| -------- | -------- |
808| Promise\<Array\<[AbilityStateData](js-apis-inner-application-abilityStateData-sys.md)>> | Promise used to return the API call result and the UIAbility information. You can perform error handling or custom processing in it.|
809
810**Error codes**
811
812| ID| Error Message|
813| ------- | -------- |
814| 16000050 | Internal error. |
815
816For details about the error codes, see [Ability Error Codes](errorcode-ability.md).
817
818**Example**
819
820```ts
821import abilityManager from '@ohos.app.ability.abilityManager';
822import { BusinessError } from '@ohos.base';
823
824abilityManager.getForegroundUIAbilities().then((data: Array<abilityManager.AbilityStateData>) => {
825    console.log(`Get foreground ui abilities data is: ${JSON.stringify(data)}`);
826}).catch((error: BusinessError) => {
827    console.error(`Get foreground ui abilities failed, error: ${JSON.stringify(error)}`);
828});
829```
830