js-apis-app-ability-abilityManager.md 14.0 KB
Newer Older
1
# @ohos.app.ability.abilityManager (AbilityManager)
2

3
The **AbilityManager** module provides APIs for obtaining, adding, and updating ability running information and state information.
4 5 6 7 8 9 10 11 12

> **NOTE**
>
> 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. 
> The APIs of this module are system APIs and cannot be called by third-party applications.

## Modules to Import

```ts
13
import abilityManager from '@ohos.app.ability.abilityManager';
14 15 16 17
```

## AbilityState

18
Enumerates the ability states. This enum can be used together with [AbilityRunningInfo](js-apis-inner-application-abilityRunningInfo.md) to return the ability state.
19 20 21

**System capability**: SystemCapability.Ability.AbilityRuntime.Core

22
**System API**: This enum is an internal definition of a system API and cannot be called by third-party applications.
23 24 25 26

| Name| Value| Description| 
| -------- | -------- | -------- |
| INITIAL | 0 | The ability is in the initial state.| 
27
| FOCUS | 2 | The ability has the focus.|
28 29
| FOREGROUND | 9 | The ability is in the foreground state. | 
| BACKGROUND | 10 | The ability is in the background state. | 
30 31
| FOREGROUNDING | 11 | The ability is in the state of being switched to the foreground. | 
| BACKGROUNDING | 12 | The ability is in the state of being switched to the background. | 
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46

## updateConfiguration

updateConfiguration(config: Configuration, callback: AsyncCallback\<void>): void

Updates the configuration. This API uses an asynchronous callback to return the result.

**Permission required**: ohos.permission.UPDATE_CONFIGURATION

**System capability**: SystemCapability.Ability.AbilityRuntime.Core
 
**Parameters**

| Name       | Type                                      | Mandatory  | Description            |
| --------- | ---------------------------------------- | ---- | -------------- |
47 48 49 50 51 52 53 54 55 56
| config    | [Configuration](js-apis-app-ability-configuration.md)   | Yes   | New configuration. You only need to configure the items to be updated.|
| callback  | AsyncCallback\<void>                   | Yes   | Callback used to return the API call result. You can perform error handling or custom processing in this callback.     |

**Error codes**

| ID| Error Message|
| ------- | -------- |
| 16000050 | Internal error. |

For details about the error codes, see [Ability Error Codes](../errorcodes/errorcode-ability.md).
57 58 59 60

**Example**

```ts
61
import abilityManager from '@ohos.app.ability.abilityManager';
62

63 64 65 66
const config = {
  language: 'Zh-Hans',                 // Simplified Chinese.
  colorMode: COLOR_MODE_LIGHT,         // Light theme.
  direction: DIRECTION_VERTICAL,       // Vertical direction.
67
  screenDensity: SCREEN_DENSITY_SDPI,  // The screen pixel density is 'sdpi'.
68 69 70
  displayId: 1,                        // The application is displayed on the display with ID 1.
  hasPointerDevice: true,              // A pointer device is connected.
};
71 72

try {
73
    abilityManager.updateConfiguration(config, (err) => {
74
        if (err && err.code !== 0) {
75
            console.error('updateConfiguration fail, err: ${JSON.stringify(err)}');
76
        } else {
77
            console.log('updateConfiguration success.');
78
        }
79
    });
80
} catch (paramError) {
81
    console.error('error.code: ${JSON.stringify(paramError.code)}, error.message: ${JSON.stringify(paramError.message)}');
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
}
```

## updateConfiguration

updateConfiguration(config: Configuration): Promise\<void>

Updates the configuration. This API uses a promise to return the result.

**Permission required**: ohos.permission.UPDATE_CONFIGURATION

**System capability**: SystemCapability.Ability.AbilityRuntime.Core

**Parameters**

| Name       | Type                                      | Mandatory  | Description            |
| --------- | ---------------------------------------- | ---- | -------------- |
99
| config    | [Configuration](js-apis-app-ability-configuration.md)   | Yes   | New configuration. You only need to configure the items to be updated.|
100 101 102 103 104

**Return value**

| Type                                      | Description     |
| ---------------------------------------- | ------- |
105 106 107 108 109 110 111 112 113
| Promise\<void> | Promise used to return the API call result. You can perform error handling or custom processing in this callback.|

**Error codes**

| ID| Error Message|
| ------- | -------- |
| 16000050 | Internal error. |

For details about the error codes, see [Ability Error Codes](../errorcodes/errorcode-ability.md).
114 115 116 117

**Example**

```ts
118
import abilityManager from '@ohos.app.ability.abilityManager';
119

120 121 122 123
const config = {
  language: 'Zh-Hans',                 // Simplified Chinese.
  colorMode: COLOR_MODE_LIGHT,         // Light theme.
  direction: DIRECTION_VERTICAL,       // Vertical direction.
124
  screenDensity: SCREEN_DENSITY_SDPI,  // The screen pixel density is 'sdpi'.
125 126 127
  displayId: 1,                        // The application is displayed on the display with ID 1.
  hasPointerDevice: true,              // A pointer device is connected.
};
128 129

try {
130 131 132
    abilityManager.updateConfiguration(config).then(() => {
        console.log('updateConfiguration success.');
    }).catch((err) => {
133
        console.error('updateConfiguration fail, err: ${JSON.stringify(err)}');
134
    });
135
} catch (paramError) {
136
    console.error('error.code: ${JSON.stringify(paramError.code)}, error.message: ${JSON.stringify(paramError.message)}');
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
}
```

## getAbilityRunningInfos

getAbilityRunningInfos(callback: AsyncCallback\<Array\<AbilityRunningInfo>>): void

Obtains the ability running information. This API uses an asynchronous callback to return the result.

**Required permissions**: ohos.permission.GET_RUNNING_INFO

**System capability**: SystemCapability.Ability.AbilityRuntime.Core

**Parameters**

| Name       | Type                                      | Mandatory  | Description            |
| --------- | ---------------------------------------- | ---- | -------------- |
154 155 156 157 158 159 160 161 162
| 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 this callback.     |

**Error codes**

| ID| Error Message|
| ------- | -------- |
| 16000050 | Internal error. |

For details about the error codes, see [Ability Error Codes](../errorcodes/errorcode-ability.md).
163 164 165 166

**Example**

```ts
167
import abilityManager from '@ohos.app.ability.abilityManager';
168 169

try {
170
    abilityManager.getAbilityRunningInfos((err, data) => {
171
        if (err && err.code !== 0) {
172
            console.error('getAbilityRunningInfos fail, error: ${JSON.stringify(err)}');
173
        } else {
174
            console.log('getAbilityRunningInfos success, data: ${JSON.stringify(data)}');
175 176
        }
    });
177
} catch (paramError) {
178
    console.error('error.code: ${JSON.stringify(paramError.code)}, error.message: ${JSON.stringify(paramError.message)}');
179 180 181 182 183 184 185
}
```

## getAbilityRunningInfos

getAbilityRunningInfos(): Promise\<Array\<AbilityRunningInfo>>

186
Obtains the UIAbility running information. This API uses a promise to return the result.
187 188 189 190 191 192 193 194 195

**Required permissions**: ohos.permission.GET_RUNNING_INFO

**System capability**: SystemCapability.Ability.AbilityRuntime.Core

**Return value**

| Type                                      | Description     |
| ---------------------------------------- | ------- |
196
| 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.|
197 198 199 200 201 202 203 204

**Error codes**

| ID| Error Message|
| ------- | -------- |
| 16000050 | Internal error. |

For details about the error codes, see [Ability Error Codes](../errorcodes/errorcode-ability.md).
205 206 207 208

**Example**

```ts
209 210
import abilityManager from '@ohos.app.ability.abilityManager';

211
try {
212
    abilityManager.getAbilityRunningInfos().then((data) => {
213
        console.log('getAbilityRunningInfos success, data: ${JSON.stringify(data)}');
214
    }).catch((err) => {
215
        console.error('getAbilityRunningInfos fail, err: ${JSON.stringify(err)}');
216
    });
217
} catch (paramError) {
218
    console.error('error.code: ${JSON.stringify(paramError.code)}, error.message: ${JSON.stringify(paramError.message)}');
219 220 221 222 223 224 225
}
```

## getExtensionRunningInfos

getExtensionRunningInfos(upperLimit: number, callback: AsyncCallback\<Array\<ExtensionRunningInfo>>): void

226
Obtains the ExtensionAbility running information. This API uses an asynchronous callback to return the result.
227 228 229 230 231 232 233 234 235

**Required permissions**: ohos.permission.GET_RUNNING_INFO

**System capability**: SystemCapability.Ability.AbilityRuntime.Core

**Parameters**

| Name       | Type                                      | Mandatory  | Description            |
| --------- | ---------------------------------------- | ---- | -------------- |
236 237 238 239 240 241 242 243 244 245
| upperLimit | number                                   | Yes| Maximum number of messages that can be obtained. The maximum value is 2<sup>31</sup>-1.|
| 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.     |

**Error codes**

| ID| Error Message|
| ------- | -------- |
| 16000050 | Internal error. |

For details about the error codes, see [Ability Error Codes](../errorcodes/errorcode-ability.md).
246 247 248 249

**Example**

```ts
250
import abilityManager from '@ohos.app.ability.abilityManager';
251

252
let upperLimit = 10;
253 254

try {
255
    abilityManager.getExtensionRunningInfos(upperLimit, (err, data) => { 
256
        if (err && err.code !== 0) {
257
            console.error('getExtensionRunningInfos fail, err: ${JSON.stringify(err)}');
258
        } else {
259
            console.log('getExtensionRunningInfos success, data: ${JSON.stringify(data)}');
260 261
        }
    });
262
} catch (paramError) {
263
    console.error('error.code: ${JSON.stringify(paramError.code)}, error.message: ${JSON.stringify(paramError.message)}');
264 265 266 267 268 269 270
}
```

## getExtensionRunningInfos

getExtensionRunningInfos(upperLimit: number): Promise\<Array\<ExtensionRunningInfo>>

271
Obtains the ExtensionAbility running information. This API uses a promise to return the result.
272 273 274 275 276 277 278 279 280
 
**Required permissions**: ohos.permission.GET_RUNNING_INFO

**System capability**: SystemCapability.Ability.AbilityRuntime.Core

**Parameters**

| Name       | Type                                      | Mandatory  | Description            |
| --------- | ---------------------------------------- | ---- | -------------- |
281
| upperLimit | number                                   | Yes| Maximum number of messages that can be obtained. The maximum value is 2<sup>31</sup>-1.|
282 283 284 285 286

**Return value**

| Type                                      | Description     |
| ---------------------------------------- | ------- |
287 288 289 290 291 292 293 294 295
| 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.|

**Error codes**

| ID| Error Message|
| ------- | -------- |
| 16000050 | Internal error. |

For details about the error codes, see [Ability Error Codes](../errorcodes/errorcode-ability.md).
296 297 298 299

**Example**

```ts
300
import abilityManager from '@ohos.app.ability.abilityManager';
301

302
let upperLimit = 10;
303 304

try {
305
    abilityManager.getExtensionRunningInfos(upperLimit).then((data) => {
306
        console.log('getExtensionRunningInfos success, data: ${JSON.stringify(data)}');
307
    }).catch((err) => {
308
        console.error('getExtensionRunningInfos fail, err: ${JSON.stringify(err)}');
309
    });
310
} catch (paramError) {
311
    console.error('error.code: ${JSON.stringify(paramError.code)}, error.message: ${JSON.stringify(paramError.message)}');
312 313 314 315 316 317 318 319 320 321 322 323 324 325 326
}
```

## getTopAbility<sup>9+</sup>

getTopAbility(callback: AsyncCallback\<ElementName>): void;

Obtains the top ability, which is the ability that has the window focus. This API uses an asynchronous callback to return the result.

**System capability**: SystemCapability.Ability.AbilityRuntime.Core

**Parameters**

| Name       | Type                                      | Mandatory  | Description            |
| --------- | ---------------------------------------- | ---- | -------------- |
327 328 329 330 331 332 333 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.     |

**Error codes**

| ID| Error Message|
| ------- | -------- |
| 16000050 | Internal error. |

For details about the error codes, see [Ability Error Codes](../errorcodes/errorcode-ability.md).
336 337 338 339

**Example**

```ts
340 341 342
import abilityManager from '@ohos.app.ability.abilityManager';

abilityManager.getTopAbility((err, data) => { 
343
    if (err && err.code !== 0) {
344
        console.error('getTopAbility fail, err: ${JSON.stringify(err)}');
345
    } else {
346
        console.log('getTopAbility success, data: ${JSON.stringify(data)}');
347 348
    }
});
349 350 351 352 353 354 355 356 357 358 359 360 361 362
```

## getTopAbility

getTopAbility(): Promise\<ElementName>;

Obtains the top ability, which is the ability that has the window focus. This API uses a promise to return the result.
 
**System capability**: SystemCapability.Ability.AbilityRuntime.Core

**Return value**

| Type                                      | Description     |
| ---------------------------------------- | ------- |
363 364 365 366 367 368 369 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.|

**Error codes**

| ID| Error Message|
| ------- | -------- |
| 16000050 | Internal error. |

For details about the error codes, see [Ability Error Codes](../errorcodes/errorcode-ability.md).
372 373 374 375

**Example**

```ts
376
import abilityManager from '@ohos.app.ability.abilityManager';
377

378
abilityManager.getTopAbility().then((data) => {
379
    console.log('getTopAbility success, data: ${JSON.stringify(data)}');
380
}).catch((err) => {
381
    console.error('getTopAbility fail, err: ${JSON.stringify(err)}');
382
});
383
```