js-apis-inner-application-appStateData.md 1.7 KB
Newer Older
1 2
# AppStateData

3
The **AppStateData** module defines the application state data, which can be obtained through [getForegroundApplications](js-apis-app-ability-appManager.md#appmanagergetforegroundapplications).
4 5 6

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

7
**System API**: This module is marked as @systemapi and not visible to third-party applications.
8

9 10 11 12 13
| Name                     | Type  | Mandatory | Description      |
| ------------------------- | ------ | ---- | --------- |
| bundleName<sup>8+</sup>   | string | No  | Bundle name.|
| uid<sup>8+</sup>          | number | No  | UID of the application.  |
| state<sup>8+</sup>        | number | No  | Application state.<br>**0**: The application is being initialized.<br>**1**: The application has been initialized and is ready.<br>**2**: The application is running in the foreground.<br>**3**: The application is having the focus. (This state is reserved.)<br>**4**: The application is running in the background.<br>**5**: The application has exited.|
14 15

**Example**
16

17
```ts
18
import appManager from "@ohos.app.ability.appManager"
19

20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
function getForegroundAppInfos() {
    appManager.getForegroundApplications((error, data) => {
        if (error && error.code) {
            console.log('getForegroundApplications failed, error.code: ' + JSON.stringify(error.code) +
            ' error.message: ' + JSON.stringify(error.message));
            return;
        }
        for (let i = 0; i < data.length; i++) {
            let appStateData = data[i];
            console.log('appStateData.bundleName: ' + appStateData.bundleName);
            console.log('appStateData.uid: ' + appStateData.uid);
            console.log('appStateData.state: ' + appStateData.state);
        }
    });
}
35
```