• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# @ohos.app.ability.abilityManager (AbilityManager)
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.md) to return the ability state.
19
20**System capability**: SystemCapability.Ability.AbilityRuntime.Core
21
22**System API**: This enum is an internal definition of a system API and cannot be called by third-party applications.
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**Permission required**: ohos.permission.UPDATE_CONFIGURATION
40
41**System capability**: SystemCapability.Ability.AbilityRuntime.Core
42
43**Parameters**
44
45| Name       | Type                                      | Mandatory  | Description            |
46| --------- | ---------------------------------------- | ---- | -------------- |
47| config    | [Configuration](js-apis-app-ability-configuration.md)   | Yes   | New configuration. You only need to configure the items to be updated.|
48| callback  | AsyncCallback\<void>                   | Yes   | Callback used to return the API call result. You can perform error handling or custom processing in this callback.     |
49
50**Error codes**
51
52| ID| Error Message|
53| ------- | -------- |
54| 16000050 | Internal error. |
55
56For details about the error codes, see [Ability Error Codes](../errorcodes/errorcode-ability.md).
57
58**Example**
59
60```ts
61import abilityManager from '@ohos.app.ability.abilityManager';
62import ConfigurationConstant from '@ohos.app.ability.ConfigurationConstant';
63
64const config = {
65  language: 'Zh-Hans',                 // Simplified Chinese.
66  colorMode: ConfigurationConstant.ColorMode.COLOR_MODE_LIGHT,         // Light theme.
67  direction: ConfigurationConstant.Direction.DIRECTION_VERTICAL,       // Vertical direction.
68  screenDensity: ConfigurationConstant.ScreenDensity.SCREEN_DENSITY_SDPI,  // The screen pixel density is 'sdpi'.
69  displayId: 1,                        // The application is displayed on the display with ID 1.
70  hasPointerDevice: true,              // A pointer device is connected.
71};
72
73try {
74    abilityManager.updateConfiguration(config, (err) => {
75        if (err.code !== 0) {
76            console.log('updateConfiguration fail, err: ' + JSON.stringify(err));
77        } else {
78            console.log('updateConfiguration success.');
79        }
80    });
81} catch (paramError) {
82    console.log('error.code: ' + JSON.stringify(paramError.code)
83        + ' error.message: ' + JSON.stringify(paramError.message));
84}
85```
86
87## updateConfiguration
88
89updateConfiguration(config: Configuration): Promise\<void>
90
91Updates the configuration. This API uses a promise to return the result.
92
93**Permission required**: ohos.permission.UPDATE_CONFIGURATION
94
95**System capability**: SystemCapability.Ability.AbilityRuntime.Core
96
97**Parameters**
98
99| Name       | Type                                      | Mandatory  | Description            |
100| --------- | ---------------------------------------- | ---- | -------------- |
101| config    | [Configuration](js-apis-app-ability-configuration.md)   | Yes   | New configuration. You only need to configure the items to be updated.|
102
103**Return value**
104
105| Type                                      | Description     |
106| ---------------------------------------- | ------- |
107| Promise\<void> | Promise used to return the API call result. You can perform error handling or custom processing in this callback.|
108
109**Error codes**
110
111| ID| Error Message|
112| ------- | -------- |
113| 16000050 | Internal error. |
114
115For details about the error codes, see [Ability Error Codes](../errorcodes/errorcode-ability.md).
116
117**Example**
118
119```ts
120import abilityManager from '@ohos.app.ability.abilityManager';
121import ConfigurationConstant from '@ohos.app.ability.ConfigurationConstant';
122
123const config = {
124  language: 'Zh-Hans',                 // Simplified Chinese.
125  colorMode: ConfigurationConstant.ColorMode.COLOR_MODE_LIGHT,         // Light theme.
126  direction: ConfigurationConstant.Direction.DIRECTION_VERTICAL,       // Vertical direction.
127  screenDensity: ConfigurationConstant.ScreenDensity.SCREEN_DENSITY_SDPI,  // The screen pixel density is 'sdpi'.
128  displayId: 1,                        // The application is displayed on the display with ID 1.
129  hasPointerDevice: true,              // A pointer device is connected.
130};
131
132try {
133    abilityManager.updateConfiguration(config).then(() => {
134        console.log('updateConfiguration success.');
135    }).catch((err) => {
136        console.log('updateConfiguration fail, err: ' + JSON.stringify(err));
137    });
138} catch (paramError) {
139    console.log('error.code: ' + JSON.stringify(paramError.code)
140        + ' error.message: ' + JSON.stringify(paramError.message));
141}
142```
143
144## getAbilityRunningInfos
145
146getAbilityRunningInfos(callback: AsyncCallback\<Array\<AbilityRunningInfo>>): void
147
148Obtains the UIAbility running information. This API uses an asynchronous callback to return the result.
149
150**Required permissions**: ohos.permission.GET_RUNNING_INFO
151
152**System capability**: SystemCapability.Ability.AbilityRuntime.Core
153
154**Parameters**
155
156| Name       | Type                                      | Mandatory  | Description            |
157| --------- | ---------------------------------------- | ---- | -------------- |
158| callback  | AsyncCallback\<Array\<[AbilityRunningInfo](js-apis-inner-application-abilityRunningInfo.md)>>  | Yes   | Callback used to return the API call result and the UIAbility running information. You can perform error handling or custom processing in this callback.     |
159
160**Error codes**
161
162| ID| Error Message|
163| ------- | -------- |
164| 16000050 | Internal error. |
165
166For details about the error codes, see [Ability Error Codes](../errorcodes/errorcode-ability.md).
167
168**Example**
169
170```ts
171import abilityManager from '@ohos.app.ability.abilityManager';
172
173try {
174    abilityManager.getAbilityRunningInfos((err, data) => {
175        if (err.code !== 0) {
176            console.log('getAbilityRunningInfos fail, error: ' + JSON.stringify(err));
177        } else {
178            console.log('getAbilityRunningInfos success, data: ' + JSON.stringify(data));
179        }
180    });
181} catch (paramError) {
182    console.log('error.code: ' + JSON.stringify(paramError.code)
183        + ' error.message: ' + JSON.stringify(paramError.message));
184}
185```
186
187## getAbilityRunningInfos
188
189getAbilityRunningInfos(): Promise\<Array\<AbilityRunningInfo>>
190
191Obtains the UIAbility running information. This API uses a promise to return the result.
192
193**Required permissions**: ohos.permission.GET_RUNNING_INFO
194
195**System capability**: SystemCapability.Ability.AbilityRuntime.Core
196
197**Return value**
198
199| Type                                      | Description     |
200| ---------------------------------------- | ------- |
201| 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 this callback.|
202
203**Error codes**
204
205| ID| Error Message|
206| ------- | -------- |
207| 16000050 | Internal error. |
208
209For details about the error codes, see [Ability Error Codes](../errorcodes/errorcode-ability.md).
210
211**Example**
212
213```ts
214import abilityManager from '@ohos.app.ability.abilityManager';
215
216try {
217    abilityManager.getAbilityRunningInfos().then((data) => {
218        console.log('getAbilityRunningInfos success, data: ' + JSON.stringify(data));
219    }).catch((err) => {
220        console.log('getAbilityRunningInfos fail, err: '  + JSON.stringify(err));
221    });
222} catch (paramError) {
223    console.log('error.code: ' + JSON.stringify(paramError.code)
224        + ' error.message: ' + JSON.stringify(paramError.message));
225}
226```
227
228## getExtensionRunningInfos
229
230getExtensionRunningInfos(upperLimit: number, callback: AsyncCallback\<Array\<ExtensionRunningInfo>>): void
231
232Obtains the ExtensionAbility running information. This API uses an asynchronous callback to return the result.
233
234**Required permissions**: ohos.permission.GET_RUNNING_INFO
235
236**System capability**: SystemCapability.Ability.AbilityRuntime.Core
237
238**Parameters**
239
240| Name       | Type                                      | Mandatory  | Description            |
241| --------- | ---------------------------------------- | ---- | -------------- |
242| upperLimit | number                                   | Yes| Maximum number of messages that can be obtained. The maximum value is 2<sup>31</sup>-1.|
243| callback  | AsyncCallback\<Array\<[ExtensionRunningInfo](js-apis-inner-application-extensionRunningInfo.md)>>  | Yes   | Callback used to return the API call result and the ExtensionAbility running information. You can perform error handling or custom processing in this callback.     |
244
245**Error codes**
246
247| ID| Error Message|
248| ------- | -------- |
249| 16000050 | Internal error. |
250
251For details about the error codes, see [Ability Error Codes](../errorcodes/errorcode-ability.md).
252
253**Example**
254
255```ts
256import abilityManager from '@ohos.app.ability.abilityManager';
257
258let upperLimit = 10;
259
260try {
261    abilityManager.getExtensionRunningInfos(upperLimit, (err, data) => {
262        if (err.code !== 0) {
263            console.log('getExtensionRunningInfos fail, err: ' + JSON.stringify(err));
264        } else {
265            console.log('getExtensionRunningInfos success, data: ' + JSON.stringify(data));
266        }
267    });
268} catch (paramError) {
269    console.log('error.code: ' + JSON.stringify(paramError.code)
270        + ' error.message: ' + JSON.stringify(paramError.message));
271}
272```
273
274## getExtensionRunningInfos
275
276getExtensionRunningInfos(upperLimit: number): Promise\<Array\<ExtensionRunningInfo>>
277
278Obtains the ExtensionAbility running information. This API uses a promise to return the result.
279
280**Required permissions**: ohos.permission.GET_RUNNING_INFO
281
282**System capability**: SystemCapability.Ability.AbilityRuntime.Core
283
284**Parameters**
285
286| Name       | Type                                      | Mandatory  | Description            |
287| --------- | ---------------------------------------- | ---- | -------------- |
288| upperLimit | number                                   | Yes| Maximum number of messages that can be obtained. The maximum value is 2<sup>31</sup>-1.|
289
290**Return value**
291
292| Type                                      | Description     |
293| ---------------------------------------- | ------- |
294| Promise\<Array\<[ExtensionRunningInfo](js-apis-inner-application-extensionRunningInfo.md)>> | Promise used to return the API call result and the ExtensionAbility running information. You can perform error handling or custom processing in this callback.|
295
296**Error codes**
297
298| ID| Error Message|
299| ------- | -------- |
300| 16000050 | Internal error. |
301
302For details about the error codes, see [Ability Error Codes](../errorcodes/errorcode-ability.md).
303
304**Example**
305
306```ts
307import abilityManager from '@ohos.app.ability.abilityManager';
308
309let upperLimit = 10;
310
311try {
312    abilityManager.getExtensionRunningInfos(upperLimit).then((data) => {
313        console.log('getExtensionRunningInfos success, data: ' + JSON.stringify(data));
314    }).catch((err) => {
315        console.log('getExtensionRunningInfos fail, err: '  + JSON.stringify(err));
316    });
317} catch (paramError) {
318    console.log('error.code: ' + JSON.stringify(paramError.code)
319        + ' error.message: ' + JSON.stringify(paramError.message));
320}
321```
322
323## getTopAbility<sup>9+</sup>
324
325getTopAbility(callback: AsyncCallback\<ElementName>): void;
326
327Obtains the top ability, which is the ability that has the window focus. This API uses an asynchronous callback to return the result.
328
329**System capability**: SystemCapability.Ability.AbilityRuntime.Core
330
331**Parameters**
332
333| Name       | Type                                      | Mandatory  | Description            |
334| --------- | ---------------------------------------- | ---- | -------------- |
335| 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 this callback.     |
336
337**Error codes**
338
339| ID| Error Message|
340| ------- | -------- |
341| 16000050 | Internal error. |
342
343For details about the error codes, see [Ability Error Codes](../errorcodes/errorcode-ability.md).
344
345**Example**
346
347```ts
348import abilityManager from '@ohos.app.ability.abilityManager';
349
350abilityManager.getTopAbility((err, data) => {
351    if (err.code !== 0) {
352        console.log('getTopAbility fail, err: ' + JSON.stringify(err));
353    } else {
354        console.log('getTopAbility success, data: ' + JSON.stringify(data));
355    }
356});
357```
358
359## getTopAbility
360
361getTopAbility(): Promise\<ElementName>;
362
363Obtains the top ability, which is the ability that has the window focus. This API uses a promise to return the result.
364
365**System capability**: SystemCapability.Ability.AbilityRuntime.Core
366
367**Return value**
368
369| Type                                      | Description     |
370| ---------------------------------------- | ------- |
371| 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 this callback.|
372
373**Error codes**
374
375| ID| Error Message|
376| ------- | -------- |
377| 16000050 | Internal error. |
378
379For details about the error codes, see [Ability Error Codes](../errorcodes/errorcode-ability.md).
380
381**Example**
382
383```ts
384import abilityManager from '@ohos.app.ability.abilityManager';
385
386abilityManager.getTopAbility().then((data) => {
387    console.log('getTopAbility success, data: ' + JSON.stringify(data));
388}).catch((err) => {
389    console.log('getTopAbility fail, err: '  + JSON.stringify(err));
390});
391```
392