js-apis-app-ability-abilityManager.md 16.6 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
| 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 51 52 53 54 55 56

**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';
G
Gloria 已提交
62
import ConfigurationConstant from '@ohos.app.ability.ConfigurationConstant';
63

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

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

## 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            |
| --------- | ---------------------------------------- | ---- | -------------- |
100
| config    | [Configuration](js-apis-app-ability-configuration.md)   | Yes   | New configuration. You only need to configure the items to be updated.|
101 102 103 104 105

**Return value**

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

**Error codes**

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

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

**Example**

```ts
119
import abilityManager from '@ohos.app.ability.abilityManager';
G
Gloria 已提交
120
import ConfigurationConstant from '@ohos.app.ability.ConfigurationConstant';
121

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

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

## 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            |
| --------- | ---------------------------------------- | ---- | -------------- |
156
| 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.     |
157 158 159 160 161 162 163 164

**Error codes**

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

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

**Example**

```ts
169
import abilityManager from '@ohos.app.ability.abilityManager';
170 171

try {
172
    abilityManager.getAbilityRunningInfos((err, data) => {
173
        if (err) {
174
            console.error('getAbilityRunningInfos fail, error: ${JSON.stringify(err)}');
175
        } else {
176
            console.log('getAbilityRunningInfos success, data: ${JSON.stringify(data)}');
177 178
        }
    });
179
} catch (paramError) {
Z
zhoujun62 已提交
180
    console.error('error.code: ${paramError.code}, error.message: ${paramError.message}');
181 182 183 184 185 186 187
}
```

## getAbilityRunningInfos

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

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

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

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

**Return value**

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

**Error codes**

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

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

**Example**

```ts
211 212
import abilityManager from '@ohos.app.ability.abilityManager';

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

## getExtensionRunningInfos

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

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

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

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

**Parameters**

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

**Error codes**

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

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

**Example**

```ts
252
import abilityManager from '@ohos.app.ability.abilityManager';
253

254
let upperLimit = 10;
255 256

try {
257 258
    abilityManager.getExtensionRunningInfos(upperLimit, (err, data) => {
        if (err) {
259
            console.error('getExtensionRunningInfos fail, err: ${JSON.stringify(err)}');
260
        } else {
261
            console.log('getExtensionRunningInfos success, data: ${JSON.stringify(data)}');
262 263
        }
    });
264
} catch (paramError) {
Z
zhoujun62 已提交
265
    console.error('error.code: ${paramError.code}, error.message: ${paramError.message}');
266 267 268 269 270 271 272
}
```

## getExtensionRunningInfos

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

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

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

**Parameters**

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

**Return value**

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

**Error codes**

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

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

**Example**

```ts
302
import abilityManager from '@ohos.app.ability.abilityManager';
303

304
let upperLimit = 10;
305 306

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

## 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            |
| --------- | ---------------------------------------- | ---- | -------------- |
329
| 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.     |
330 331 332 333 334 335 336 337

**Error codes**

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

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

**Example**

```ts
342 343 344
import abilityManager from '@ohos.app.ability.abilityManager';

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

## 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     |
| ---------------------------------------- | ------- |
365
| 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.|
366 367 368 369 370 371 372 373

**Error codes**

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

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

**Example**

```ts
378
import abilityManager from '@ohos.app.ability.abilityManager';
379

380
abilityManager.getTopAbility().then((data) => {
381
    console.log('getTopAbility success, data: ${JSON.stringify(data)}');
382
}).catch((err) => {
383
    console.error('getTopAbility fail, err: ${JSON.stringify(err)}');
384
});
385
```
386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460

## acquireShareData<sup>10+</sup>

acquireShareData(missionId: number, callback: AsyncCallback<{[key: string]: Object}>): void;

Acquires the shared data of the target device. This API uses an asynchronous callback to return the result.

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

**Parameters**

| Name       | Type                                      | Mandatory  | Description            |
| --------- | ---------------------------------------- | ---- | -------------- |
| missionId | number                                   | Yes| Mission ID on the target device. The maximum value is 2<sup>31</sup>-1.|
| 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.     |

**Error codes**

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

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

**Example**

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

abilityManager.acquireShareData(1, (err, wantParam) => { 
    if (err) {
        console.error(`acquireShareData fail, err: ${JSON.stringify(err)}`);
    } else {
        console.log(`acquireShareData success, data: ${JSON.stringify(data)}`);
    }
});

```

## acquireShareData<sup>10+</sup>

acquireShareData(missionId: number): Promise<{[key: string]: Object}>;

Acquires the shared data of the target device. This API uses a promise to return the result.
 
**System capability**: SystemCapability.Ability.AbilityRuntime.Core

**Return value**

| Type                                      | Description     |
| ---------------------------------------- | ------- |
| 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.|

**Error codes**

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

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

**Example**

```ts
import abilityManager from '@ohos.app.ability.abilityManager';
try {
    abilityManager.acquireShareData(1).then((wantParam) => {
    console.log(`acquireShareData success, data: ${JSON.stringify(data)}`);
    }).catch((err) => {
    console.error(`acquireShareData fail, err: ${JSON.stringify(err)}`);
    });
} catch (paramError) {
    console.error(`error.code: ${JSON.stringify(paramError.code)}, error.message: ${JSON.stringify(paramError.message)}`);
}
```