• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# @ohos.app.ability.abilityManager (Ability Information Management)
2
3The AbilityManager module provides APIs for obtaining ability information and running status 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
9## Modules to Import
10
11```ts
12import { abilityManager } from '@kit.AbilityKit';
13```
14
15## AbilityState<sup>14+</sup>
16
17Enumerates the ability states. This enum can be used together with [AbilityRunningInfo](js-apis-inner-application-abilityRunningInfo.md) to return the ability state.
18
19**System capability**: SystemCapability.Ability.AbilityRuntime.Core
20
21| Name| Value| Description|
22| -------- | -------- | -------- |
23| INITIAL | 0 | The ability is in the initial state.|
24| FOCUS | 2 | The ability has the focus.|
25| FOREGROUND | 9 | The ability is in the foreground state. |
26| BACKGROUND | 10 | The ability is in the background state. |
27| FOREGROUNDING | 11 | The ability is in the state of being switched to the foreground. |
28| BACKGROUNDING | 12 | The ability is in the state of being switched to the background. |
29
30
31## abilityManager.getAbilityRunningInfos<sup>14+</sup>
32
33getAbilityRunningInfos(): Promise\<Array\<AbilityRunningInfo>>
34
35Obtains the UIAbility running information. This API uses a promise to return the result.
36
37**Required permissions**: ohos.permission.GET_RUNNING_INFO
38
39**System capability**: SystemCapability.Ability.AbilityRuntime.Core
40
41**Return value**
42
43| Type                                      | Description     |
44| ---------------------------------------- | ------- |
45| Promise\<Array\<[AbilityRunningInfo](js-apis-inner-application-abilityRunningInfo.md)>> | Promise used to return the API call result and the UIAbility running information. You can perform error handling or custom processing in it.|
46
47**Error codes**
48
49For details about the error codes, see [Ability Error Codes](errorcode-ability.md).
50
51| ID| Error Message|
52| ------- | -------- |
53| 16000050 | Internal error. |
54
55**Example**
56
57```ts
58import { abilityManager } from '@kit.AbilityKit';
59import { BusinessError } from '@kit.BasicServicesKit';
60
61try {
62  abilityManager.getAbilityRunningInfos()
63    .then((data: abilityManager.AbilityRunningInfo[]) => {
64      console.log(`getAbilityRunningInfos success, data: ${JSON.stringify(data)}`);
65    })
66    .catch((error: BusinessError) => {
67      console.error(`getAbilityRunningInfos fail, error code: ${JSON.stringify(error.code)}, error msg: ${JSON.stringify(error.message)}`);
68    })
69} catch (e) {
70  let code = (e as BusinessError).code;
71  let msg = (e as BusinessError).message;
72  console.error(`getAbilityRunningInfos fail, error code: ${JSON.stringify(code)}, error msg: ${JSON.stringify(msg)}`);
73}
74```
75
76## abilityManager.restartSelfAtomicService<sup>20+</sup>
77
78restartSelfAtomicService(context: Context): void
79
80Restarts the current atomic service.
81
82> **NOTE**
83>
84> - Currently, atomic services can be started only in an independent window.
85>
86> - The interval between two calls of this API cannot be less than 3 seconds.
87
88**Model restriction**: This API can be used only in the stage model.
89
90**System capability**: SystemCapability.Ability.AbilityRuntime.Core
91
92**Atomic service API**: This API can be used in atomic services since API version 20.
93
94**Parameters**
95
96| Name       | Type                                      | Mandatory  | Description            |
97| --------- | ---------------------------------------- | ---- | -------------- |
98| context    | [Context](./js-apis-inner-application-context.md)   | Yes   | Context of the ability.<br>Note: Currently, only [UIAbilityContext](js-apis-inner-application-uiAbilityContext.md) is supported.<br>|
99
100**Error codes**
101
102For details about the error codes, see [Ability Error Codes](errorcode-ability.md).
103
104| ID| Error Message|
105| ------- | -------- |
106| 16000050 | Internal error. Possible causes: 1. Connect to system service failed; 2.Send restart message to system service failed; 3.System service failed to communicate with dependency module.|
107| 16000053 | The ability is not on the top of the UI. |
108| 16000064 | Restart too frequently. Try again at least 3s later. |
109| 16000086 | The context is not UIAbilityContext. |
110| 16000090 | The caller is not an atomic service. |
111
112**Example**
113
114```ts
115import { AbilityConstant, EmbeddableUIAbility, Want, abilityManager } from '@kit.AbilityKit';
116import { BusinessError } from '@kit.BasicServicesKit';
117
118export default class EntryAbility extends EmbeddableUIAbility {
119  onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void {
120    try {
121      abilityManager.restartSelfAtomicService(this.context);
122    } catch (e) {
123      console.error(`restartSelfAtomicService error: ${JSON.stringify(e as BusinessError)}`);
124    }
125  }
126}
127```
128
129## AbilityRunningInfo<sup>14+</sup>
130
131type AbilityRunningInfo = _AbilityRunningInfo
132
133Defines the level-2 module AbilityRunningInfo.
134
135**System capability**: SystemCapability.Ability.AbilityRuntime.Core
136
137| Type| Description|
138| --- | --- |
139| [_AbilityRunningInfo](js-apis-inner-application-abilityRunningInfo.md) | AbilityRunningInfo, a level-2 module that provides the information and state related to an ability.|
140
141## AbilityStateData<sup>14+</sup>
142
143type AbilityStateData = _AbilityStateData.default
144
145Defines the level-2 module AbilityStateData.
146
147**System capability**: SystemCapability.Ability.AbilityRuntime.Core
148
149| Type| Description|
150| --- | --- |
151| [_AbilityStateData.default](js-apis-inner-application-abilityStateData.md) | AbilityStateData, a level-2 module that provides the ability state information.|
152