openharmony 鸿蒙 long-time-task-guide

2025-06-06 浏览 (1)

Continuous Task Development (TaskPool)

This section provides a development guide for continuous tasks using TaskPool, with the example of periodically collecting sensor data.

Using TaskPool to Listen for Sensor Data

  1. Import the required modules.

    // Index.ets
    import { sensor } from '@kit.SensorServiceKit';
    import { taskpool } from '@kit.ArkTS';
    import { BusinessError, emitter } from '@kit.BasicServicesKit';
    
  2. Define a continuous task to listen for sensor data internally and register a destruction notification via emitter.

    // Index.ets
    @Concurrent
    async function SensorListener() : Promise<void> {
      sensor.on(sensor.SensorId.ACCELEROMETER, (data) => {
        emitter.emit({ eventId: 0 }, { data: data });
      }, { interval: 1000000000 });
    
      emitter.on({ eventId: 1 }, () => {
        sensor.off(sensor.SensorId.ACCELEROMETER)
        emitter.off(1)
      })
    }
    
  3. Define the registration and destruction behavior in the host thread.

    • Registration: Initiate the continuous task and receive listening data via emitter.
    • Destruction: Send an event to cancel sensor listening and terminate the continuous task.
    // Index.ets
    @Entry
    @Component
    struct Index {
      sensorTask?: taskpool.LongTask
    
      build() {
        Column() {
          Text("Add listener")
            .id('HelloWorld')
            .fontSize(50)
            .fontWeight(FontWeight.Bold)
            .onClick(() => {
              this.sensorTask = new taskpool.LongTask(SensorListener);
              emitter.on({ eventId: 0 }, (data) => {
                // Do something here
                console.info(`Receive ACCELEROMETER data: {${data.data?.x}, ${data.data?.y}, ${data.data?.z}`);
              });
              taskpool.execute(this.sensorTask).then(() => {
                console.info("Add listener of ACCELEROMETER success");
              }).catch((e: BusinessError) => {
                // Process error
              })
            })
          Text("Delete listener")
            .id('HelloWorld')
            .fontSize(50)
            .fontWeight(FontWeight.Bold)
            .onClick(() => {
              emitter.emit({ eventId: 1 });
              emitter.off(0);
              if(this.sensorTask != undefined) {
                taskpool.terminateTask(this.sensorTask);
              } else {
                console.error("sensorTask is undefined.");
              }
            })
        }
        .height('100%')
        .width('100%')
      }
    }
    

你可能感兴趣的鸿蒙文章

harmony 鸿蒙ArkTS

harmony 鸿蒙Configuring arkOptions in build-profile.json5

harmony 鸿蒙Asynchronous Lock

harmony 鸿蒙Ark Bytecode File Format

harmony 鸿蒙Naming Conventions for Ark Bytecode Functions

harmony 鸿蒙Ark Bytecode Fundamentals

harmony 鸿蒙Overview of Ark Bytecode

harmony 鸿蒙Shared Container

harmony 鸿蒙Asynchronous Waiting

harmony 鸿蒙ArkTS Cross-Language Interaction

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