harmony 鸿蒙Stationary Development

2023-06-24 浏览 (961)

Stationary Development

When to Use

An application can call the Stationary module to obtain the device status, for example, whether the device is absolutely or relatively still.

For details about the APIs, see Stationary.

Device Status Type Parameters

NameDescription
stillAbsolutely still.
relativeStillRelatively still.

Parameters for Subscribing to Device Status events

NameValueDescription
ENTER1Event indicating entering device status.
EXIT2Event indicating exiting device status.
ENTER_EXIT3Event indicating entering and exiting device status.

Returned Device Status Parameters

NameValueDescription
ENTER1Entering device status.
EXIT2Exiting device status.

Available APIs

ModuleNameDescription
ohos.stationaryon(activity: ActivityType, event: ActivityEvent, reportLatencyNs: number, callback: Callback<ActivityResponse>): voidSubscribes to the device status. This API uses an asynchronous callback to return the result.
ohos.stationaryonce(activity: ActivityType, callback: Callback<ActivityResponse>): voidObtains the device status. This API uses an asynchronous callback to return the result.
ohos.stationaryoff(activity: ActivityType, event: ActivityEvent, callback?: Callback<ActivityResponse>): voidUnsubscribes from the device status.

Constraints

The device must support the acceleration sensor.

How to Develop

  1. Subscribe to the event indicating entering the absolute still state, and the event is reported every 1 second.

    import stationary from '@ohos.stationary';
    import { BusinessError } from '@ohos.base';
    let reportLatencyNs = 1000000000;
    try {
       stationary.on('still', stationary.ActivityEvent.ENTER, reportLatencyNs, (data) => {
          console.log('data='+ JSON.stringify(data));
       })
    } catch (error) {
       let message = (error as BusinessError).message;
       console.error('stationary on failed:' + message);
    }
    
  2. Obtain the event indicating entering the absolute still state.

    import stationary from '@ohos.stationary';
    import { BusinessError } from '@ohos.base';
    try {
       stationary.once('still', (data) => {
          console.log('data='+ JSON.stringify(data));
       })
    } catch (error) {
       let message = (error as BusinessError).message;
       console.error('stationary once failed:' + message);
    }
    
  3. Unsubscribe from the event indicating entering the absolute still state.

    import stationary from '@ohos.stationary';
    import { BusinessError } from '@ohos.base';
    try {
       stationary.off('still', stationary.ActivityEvent.ENTER, (data) => {
          console.log('data='+ JSON.stringify(data));
       })
    } catch (error) {
       let message = (error as BusinessError).message;
       console.error('stationary off failed:' + message);
    }
    

你可能感兴趣的鸿蒙文章

harmony 鸿蒙Device Management

harmony 鸿蒙Peripheral Management Development

harmony 鸿蒙Input Device Development

harmony 鸿蒙Location Service Development

harmony 鸿蒙Mouse Pointer Development

harmony 鸿蒙Sample Server Development

harmony 鸿蒙Sample Server Overview

harmony 鸿蒙Sensor Development

harmony 鸿蒙Sensor Overview

harmony 鸿蒙USB Service Development

  • 所属分类: 后端技术
  • 本文标签: 软件 鸿蒙
  • 版权声明: 本文链接 https://seaxiang.com/blog/4ea428cd62d542d4ab6fcf60fac93ddf