• 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 it.     |
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 { Configuration } from '@ohos.app.ability.Configuration';
63import ConfigurationConstant from '@ohos.app.ability.ConfigurationConstant';
64
65const config: Configuration = {
66  language: 'Zh-Hans',                 // Simplified Chinese.
67  colorMode: ConfigurationConstant.ColorMode.COLOR_MODE_LIGHT,         // Light theme.
68  direction: ConfigurationConstant.Direction.DIRECTION_VERTICAL,       // Vertical direction.
69  screenDensity: ConfigurationConstant.ScreenDensity.SCREEN_DENSITY_SDPI,  // The screen pixel density is 'sdpi'.
70  displayId: 1,                        // The application is displayed on the display with ID 1.
71  hasPointerDevice: true,              // A pointer device is connected.
72};
73
74try {
75    abilityManager.updateConfiguration(config, (err) => {
76        if (err) {
77            console.error(`updateConfiguration fail, err: ${JSON.stringify(err)}`);
78        } else {
79            console.log('updateConfiguration success.');
80        }
81    });
82} catch (paramError) {
83    console.error(`error.code: ${paramError.code}, error.message: ${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 it.|
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 { Configuration } from '@ohos.app.ability.Configuration';
122import ConfigurationConstant from '@ohos.app.ability.ConfigurationConstant';
123import { BusinessError } from '@ohos.base';
124
125const config: Configuration = {
126  language: 'Zh-Hans',                 // Simplified Chinese.
127  colorMode: ConfigurationConstant.ColorMode.COLOR_MODE_LIGHT,         // Light theme.
128  direction: ConfigurationConstant.Direction.DIRECTION_VERTICAL,       // Vertical direction.
129  screenDensity: ConfigurationConstant.ScreenDensity.SCREEN_DENSITY_SDPI,  // The screen pixel density is 'sdpi'.
130  displayId: 1,                        // The application is displayed on the display with ID 1.
131  hasPointerDevice: true,              // A pointer device is connected.
132};
133
134try {
135    abilityManager.updateConfiguration(config).then(() => {
136        console.log('updateConfiguration success.');
137    }).catch((err: BusinessError) => {
138        console.error(`updateConfiguration fail, err: ${JSON.stringify(err)}`);
139    });
140} catch (paramError) {
141    console.error(`error.code: ${paramError.code}, error.message: ${paramError.message}`);
142}
143```
144
145## getAbilityRunningInfos
146
147getAbilityRunningInfos(callback: AsyncCallback\<Array\<AbilityRunningInfo>>): void
148
149Obtains the UIAbility running information. This API uses an asynchronous callback to return the result.
150
151**Required permissions**: ohos.permission.GET_RUNNING_INFO
152
153**System capability**: SystemCapability.Ability.AbilityRuntime.Core
154
155**Parameters**
156
157| Name       | Type                                      | Mandatory  | Description            |
158| --------- | ---------------------------------------- | ---- | -------------- |
159| callback  | AsyncCallback\<Array\<[AbilityRunningInfo](js-apis-inner-application-abilityRunningInfo.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.     |
160
161**Error codes**
162
163| ID| Error Message|
164| ------- | -------- |
165| 16000050 | Internal error. |
166
167For details about the error codes, see [Ability Error Codes](../errorcodes/errorcode-ability.md).
168
169**Example**
170
171```ts
172import abilityManager from '@ohos.app.ability.abilityManager';
173
174try {
175    abilityManager.getAbilityRunningInfos((err, data) => {
176        if (err) {
177            console.error(`getAbilityRunningInfos fail, error: ${JSON.stringify(err)}`);
178        } else {
179            console.log(`getAbilityRunningInfos success, data: ${JSON.stringify(data)}`);
180        }
181    });
182} catch (paramError) {
183    console.error('error.code: ${paramError.code}, error.message: ${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 it.|
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';
215import { BusinessError } from '@ohos.base';
216
217try {
218    abilityManager.getAbilityRunningInfos().then((data) => {
219        console.log(`getAbilityRunningInfos success, data: ${JSON.stringify(data)}`);
220    }).catch((err: BusinessError) => {
221        console.error(`getAbilityRunningInfos fail, err: ${JSON.stringify(err)}`);
222    });
223} catch (paramError) {
224    console.error('error.code: ${paramError.code}, error.message: ${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 it.     |
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) {
263            console.error(`getExtensionRunningInfos fail, err: ${JSON.stringify(err)}`);
264        } else {
265            console.log(`getExtensionRunningInfos success, data: ${JSON.stringify(data)}`);
266        }
267    });
268} catch (paramError) {
269    console.error('error.code: ${paramError.code}, error.message: ${paramError.message}');
270}
271```
272
273## getExtensionRunningInfos
274
275getExtensionRunningInfos(upperLimit: number): Promise\<Array\<ExtensionRunningInfo>>
276
277Obtains the ExtensionAbility running information. This API uses a promise to return the result.
278
279**Required permissions**: ohos.permission.GET_RUNNING_INFO
280
281**System capability**: SystemCapability.Ability.AbilityRuntime.Core
282
283**Parameters**
284
285| Name       | Type                                      | Mandatory  | Description            |
286| --------- | ---------------------------------------- | ---- | -------------- |
287| upperLimit | number                                   | Yes| Maximum number of messages that can be obtained. The maximum value is 2<sup>31</sup>-1.|
288
289**Return value**
290
291| Type                                      | Description     |
292| ---------------------------------------- | ------- |
293| 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 it.|
294
295**Error codes**
296
297| ID| Error Message|
298| ------- | -------- |
299| 16000050 | Internal error. |
300
301For details about the error codes, see [Ability Error Codes](../errorcodes/errorcode-ability.md).
302
303**Example**
304
305```ts
306import abilityManager from '@ohos.app.ability.abilityManager';
307import { BusinessError } from '@ohos.base';
308
309let upperLimit = 10;
310
311try {
312    abilityManager.getExtensionRunningInfos(upperLimit).then((data) => {
313        console.log(`getExtensionRunningInfos success, data: ${JSON.stringify(data)}`);
314    }).catch((err: BusinessError) => {
315        console.error(`getExtensionRunningInfos fail, err: ${JSON.stringify(err)}`);
316    });
317} catch (paramError) {
318    console.error('error.code: ${paramError.code}, error.message: ${paramError.message}');
319}
320```
321
322## getTopAbility<sup>9+</sup>
323
324getTopAbility(callback: AsyncCallback\<ElementName>): void;
325
326Obtains the top ability, which is the ability that has the window focus. This API uses an asynchronous callback to return the result.
327
328**System capability**: SystemCapability.Ability.AbilityRuntime.Core
329
330**Parameters**
331
332| Name       | Type                                      | Mandatory  | Description            |
333| --------- | ---------------------------------------- | ---- | -------------- |
334| 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.     |
335
336**Error codes**
337
338| ID| Error Message|
339| ------- | -------- |
340| 16000050 | Internal error. |
341
342For details about the error codes, see [Ability Error Codes](../errorcodes/errorcode-ability.md).
343
344**Example**
345
346```ts
347import abilityManager from '@ohos.app.ability.abilityManager';
348
349abilityManager.getTopAbility((err, data) => {
350    if (err) {
351        console.error(`getTopAbility fail, err: ${JSON.stringify(err)}`);
352    } else {
353        console.log(`getTopAbility success, data: ${JSON.stringify(data)}`);
354    }
355});
356```
357
358## getTopAbility
359
360getTopAbility(): Promise\<ElementName>;
361
362Obtains the top ability, which is the ability that has the window focus. This API uses a promise to return the result.
363
364**System capability**: SystemCapability.Ability.AbilityRuntime.Core
365
366**Return value**
367
368| Type                                      | Description     |
369| ---------------------------------------- | ------- |
370| 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.|
371
372**Error codes**
373
374| ID| Error Message|
375| ------- | -------- |
376| 16000050 | Internal error. |
377
378For details about the error codes, see [Ability Error Codes](../errorcodes/errorcode-ability.md).
379
380**Example**
381
382```ts
383import abilityManager from '@ohos.app.ability.abilityManager';
384import { BusinessError } from '@ohos.base';
385
386abilityManager.getTopAbility().then((data) => {
387    console.log(`getTopAbility success, data: ${JSON.stringify(data)}`);
388}).catch((err: BusinessError) => {
389    console.error(`getTopAbility fail, err: ${JSON.stringify(err)}`);
390});
391```
392
393## acquireShareData<sup>10+</sup>
394
395acquireShareData(missionId: number, callback: AsyncCallback<{[key: string]: Object}>): void;
396
397Called 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.
398
399**System capability**: SystemCapability.Ability.AbilityRuntime.Core
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| missionId | number                                   | Yes| Mission ID on the target application. The maximum value is 2<sup>31</sup>-1.|
408| callback  | AsyncCallback<{[key: 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.     |
409
410**Error codes**
411
412| ID| Error Message|
413| ------- | -------- |
414| 16000050 | Internal error. |
415
416For details about the error codes, see [Ability Error Codes](../errorcodes/errorcode-ability.md).
417
418**Example**
419
420```ts
421import abilityManager from '@ohos.app.ability.abilityManager';
422import { BusinessError } from '@ohos.base';
423
424try {
425    abilityManager.acquireShareData(1, (err, wantParam) => {
426        if (err) {
427            console.error(`acquireShareData fail, err: ${JSON.stringify(err)}`);
428        } else {
429            console.log(`acquireShareData success, data: ${JSON.stringify(wantParam)}`);
430        }
431    });
432} catch (paramError) {
433    let code = (paramError as BusinessError).code;
434    let message = (paramError as BusinessError).message;
435    console.error(`error.code: ${JSON.stringify(code)}, error.message: ${JSON.stringify(message)}`);
436}
437
438```
439
440## acquireShareData<sup>10+</sup>
441
442acquireShareData(missionId: number): Promise<{[key: string]: Object}>;
443
444Called 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.
445
446**System capability**: SystemCapability.Ability.AbilityRuntime.Core
447
448**System API**: This is a system API and cannot be called by third-party applications.
449
450**Parameters**
451
452| Name       | Type                                      | Mandatory  | Description            |
453| --------- | ---------------------------------------- | ---- | -------------- |
454| missionId | number                                   | Yes| Mission ID on the target application. The maximum value is 2<sup>31</sup>-1.|
455
456**Return value**
457
458| Type                                      | Description     |
459| ---------------------------------------- | ------- |
460| Promise<{[key: string]: Object}>| Promise used to return the API call result and the shared data. You can perform error handling or custom processing in it.|
461
462**Error codes**
463
464| ID| Error Message|
465| ------- | -------- |
466| 16000050 | Internal error. |
467
468For details about the error codes, see [Ability Error Codes](../errorcodes/errorcode-ability.md).
469
470**Example**
471
472```ts
473import abilityManager from '@ohos.app.ability.abilityManager';
474import { BusinessError } from '@ohos.base';
475
476try {
477    abilityManager.acquireShareData(1).then((wantParam) => {
478    console.log(`acquireShareData success, data: ${JSON.stringify(wantParam)}`);
479    }).catch((err: BusinessError) => {
480    console.error(`acquireShareData fail, err: ${JSON.stringify(err)}`);
481    });
482} catch (paramError) {
483    let code = (paramError as BusinessError).code;
484    let message = (paramError as BusinessError).message;
485    console.error(`error.code: ${JSON.stringify(code)}, error.message: ${JSON.stringify(message)}`);
486}
487```
488
489## notifySaveAsResult<sup>10+</sup>
490
491notifySaveAsResult(parameter: AbilityResult, requestCode: number, callback: AsyncCallback\<void>): void;
492
493Used by the Data Loss Prevention (DLP) management application to notify a sandbox application of the data saving result. This API uses an asynchronous callback to return the result.
494
495**Model restriction**: This API can be used only in the stage model.
496
497**System capability**: SystemCapability.Ability.AbilityRuntime.Core
498
499**System API**: This is a system API.
500
501**Parameters**
502
503| Name       | Type                                      | Mandatory  | Description            |
504| --------- | ---------------------------------------- | ---- | -------------- |
505| parameter | [AbilityResult](js-apis-inner-ability-abilityResult.md) | Yes| Information returned to the initiator UIAbility.|
506| requestCode | number                                        | Yes| Request code passed in by the DLP management application.         |
507| callback  | AsyncCallback<void>                             | Yes| Callback used to return the result. If the operation is successful, **err** is **undefined**; otherwise, **err** is an error object.        |
508
509**Error codes**
510
511| ID| Error Message|
512| ------- | -------- |
513| 16000050 | Internal error. |
514
515For details about the error codes, see [Ability Error Codes](../errorcodes/errorcode-ability.md).
516
517**Example**
518
519```ts
520import abilityManager from '@ohos.app.ability.abilityManager';
521import common from '@ohos.app.ability.common';
522import Want from '@ohos.app.ability.Want';
523import { BusinessError } from '@ohos.base';
524let want: Want = {
525  bundleName: 'com.example.myapplication',
526  abilityName: 'EntryAbility'
527};
528let resultCode = 100;
529// AbilityResult information returned to the initiator of the save-as behavior.
530let abilityResult: common.AbilityResult = {
531    want,
532    resultCode
533};
534let requestCode = 1;
535try {
536  abilityManager.notifySaveAsResult(abilityResult, requestCode, (err) => {
537    if (err && err.code != 0) {
538      console.error(`notifySaveAsResult fail, err: ${JSON.stringify(err)}`);
539    } else {
540      console.log(`notifySaveAsResult success`);
541    }
542  });
543} catch (paramError) {
544  let code = (paramError as BusinessError).code;
545  let message = (paramError as BusinessError).message;
546  console.error(`error.code: ${JSON.stringify(code)}, error.message: ${JSON.stringify(message)}`);
547}
548```
549
550## notifySaveAsResult<sup>10+</sup>
551
552notifySaveAsResult(parameter: AbilityResult, requestCode: number): Promise\<void>;
553
554Used by the DLP management application to notify a sandbox application of the data saving result. This API uses a promise to return the result.
555
556**Model restriction**: This API can be used only in the stage model.
557
558**System capability**: SystemCapability.Ability.AbilityRuntime.Core
559
560**System API**: This is a system API.
561
562**Parameters**
563
564| Name       | Type                                      | Mandatory  | Description            |
565| --------- | ---------------------------------------- | ---- | -------------- |
566| parameter | [AbilityResult](js-apis-inner-ability-abilityResult.md) | Yes| Information returned to the initiator UIAbility.|
567| requestCode | number                                        | Yes| Request code passed in by the DLP management application.         |
568
569**Return value**
570
571| Type                                      | Description     |
572| ---------------------------------------- | ------- |
573| Promise<void>| Promise that returns no value.|
574
575**Error codes**
576
577| ID| Error Message|
578| ------- | -------- |
579| 16000050 | Internal error. |
580
581For details about the error codes, see [Ability Error Codes](../errorcodes/errorcode-ability.md).
582
583**Example**
584
585```ts
586import abilityManager from '@ohos.app.ability.abilityManager';
587import common from '@ohos.app.ability.common';
588import Want from '@ohos.app.ability.Want';
589import { BusinessError } from '@ohos.base';
590let want: Want = {
591  bundleName: 'com.example.myapplication',
592  abilityName: 'EntryAbility'
593};
594let resultCode = 100;
595// AbilityResult information returned to the initiator of the save-as behavior.
596let abilityResult: common.AbilityResult = {
597    want,
598    resultCode
599};
600let requestCode = 1;
601try {
602  abilityManager.notifySaveAsResult(abilityResult, requestCode).catch((err) => {
603    console.error(`notifySaveAsResult fail, err: ${JSON.stringify(err)}`);
604  }).then(() => {
605    console.log(`notifySaveAsResult success`);
606  });
607} catch (paramError) {
608  let code = (paramError as BusinessError).code;
609  let message = (paramError as BusinessError).message;
610  console.error(`error.code: ${JSON.stringify(code)}, error.message: ${JSON.stringify(message)}`);
611}
612```
613