• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Stationary Development
2
3
4## When to Use
5
6An application can call the **Stationary** module to obtain the device status, for example, whether the device is absolutely or relatively still.
7
8For details about the APIs, see [Stationary](../reference/apis/js-apis-stationary.md).
9
10## Device Status Type Parameters
11
12| Name| Description|
13| -------- | -------- |
14| still | Absolutely still.|
15| relativeStill | Relatively still.|
16
17## Parameters for Subscribing to Device Status events
18
19| Name                            | Value   | Description                                      |
20| ------------------------------ | ---- | ---------------------------------------- |
21| ENTER         | 1    | Event indicating entering device status.  |
22| EXIT | 2   | Event indicating exiting device status.|
23| ENTER_EXIT | 3   | Event indicating entering and exiting device status.|
24
25## Returned Device Status Parameters
26
27| Name                            | Value   | Description                                      |
28| ------------------------------ | ---- | ---------------------------------------- |
29| ENTER         | 1    | Entering device status.  |
30| EXIT | 2   | Exiting device status.|
31
32## Available APIs
33
34| Module         | Name                                                      | Description                                                        |
35| ------------- | ------------------------------------------------------------ | ------------------------------------------------------------ |
36| ohos.stationary | on(activity: ActivityType, event: ActivityEvent, reportLatencyNs: number, callback: Callback<ActivityResponse>): void | Subscribes to the device status. This API uses an asynchronous callback to return the result.|
37| ohos.stationary | once(activity: ActivityType, callback: Callback<ActivityResponse>): void | Obtains the device status. This API uses an asynchronous callback to return the result.|
38| ohos.stationary | off(activity: ActivityType, event: ActivityEvent, callback?: Callback<ActivityResponse>): void | Unsubscribes from the device status.                                |
39
40## Constraints
41
42The device must support the acceleration sensor.
43
44## How to Develop
45
461. Subscribe to the event indicating entering the absolute still state, and the event is reported every 1 second.
47
48   ```ts
49   import stationary from '@ohos.stationary';
50   import { BusinessError } from '@ohos.base';
51   let reportLatencyNs = 1000000000;
52   try {
53      stationary.on('still', stationary.ActivityEvent.ENTER, reportLatencyNs, (data) => {
54         console.log('data='+ JSON.stringify(data));
55      })
56   } catch (error) {
57      let message = (error as BusinessError).message;
58      console.error('stationary on failed:' + message);
59   }
60   ```
61
622. Obtain the event indicating entering the absolute still state.
63
64   ```ts
65   import stationary from '@ohos.stationary';
66   import { BusinessError } from '@ohos.base';
67   try {
68      stationary.once('still', (data) => {
69         console.log('data='+ JSON.stringify(data));
70      })
71   } catch (error) {
72      let message = (error as BusinessError).message;
73      console.error('stationary once failed:' + message);
74   }
75   ```
76
773. Unsubscribe from the event indicating entering the absolute still state.
78
79   ```ts
80   import stationary from '@ohos.stationary';
81   import { BusinessError } from '@ohos.base';
82   try {
83      stationary.off('still', stationary.ActivityEvent.ENTER, (data) => {
84         console.log('data='+ JSON.stringify(data));
85      })
86   } catch (error) {
87      let message = (error as BusinessError).message;
88      console.error('stationary off failed:' + message);
89   }
90   ```
91