Background Service
Introduction
Prior to Zepp OS version 3.0, once a JS Mini Program was exited, the Mini Program was destroyed, and if it needed to save the associated state, it needed to rely on file system-related capabilities and could not be run in the background for scenarios such as continuous monitoring of sensor data.
In Zepp OS 3.0, Mini Programs support the new feature of background services, which differentiate services in terms of runtime, divided into long services and short services.
Long services, started in the Mini Program page via the start
method in the @zos/app-service
module, are allowed to reside in the background and are automatically loaded upon restart until the service is stopped using the stop
method.
Short services, i.e. services that run for a short period of time and are forcibly shut down if they are started beyond a certain time. Analogous to callback functions, they are triggered by specific events, such as the new series of module APIs added to Zepp OS that support calling short services.
@zos/alarm
- Timed wake-up, support for waking up short services
@zos/notification
- OS notifications, evoking short services
app-event
System Events- Listening to system events and calling up short services when an event is thrown
Configure backend services
You need to add the app.json to the Mini Program configuration. In addition, the background service has permission restrictions, so you need to declare the relevant permissions in permissions
.
{
"permissions": [
"device:os.notification",
"device:os.bg_service",
"event:os.health.heart_rate_abnl"
],
"module": {
"app-service": {
"services": ["app-service/long_service"]
},
"app-event": {
"path": "app-event/short-service"
}
}
}
A Mini Program can configure multiple app-services
to start long services, which need to be manually controlled by start
and stop
APIs to start and stop
A Mini Program can only configure one short service to listen to system events, and the path of the service is specified by path
of app-event
, when the system event we listen to is triggered, the system will call the short service and close the service after a certain time
Register for background services
Use AppService
register background service.
Ability Limits
Calling JS methods in the backend service cannot affect the frontend application, and only the following API calls are supported
showToast
of the@zos/interaction
module@zos/sensor
@zos/fs
@zos/ble
@zos/router
@zos/alarm
@zos/app-service
@zos/notification
- The rest of the module has a series of
get
methods
Short Service System Events
The system events that the Mini Program needs to listen to also need to be configured in the app.json permissions
field, which currently supports the following event listeners
System Events | Description |
---|---|
event:os.health.heart_rate_abnl | Abnormal heart rate |
event:os.health.spo2_low | Low blood oxygen |
event:os.weather.sun_rise | Sunrise |
event:os.weather.sun_rise | Sunset |
event:os.weather.moon_rise | Moonrise |
event:os.weather.moon_set | Moonset |
The short service that listens to system events also needs to be configured in the app-event field in app.json
"app-service": {
"services": ["app-service/long_service"]
},
"app-event": {
"path": "app-event/short_service"
}
More system events are still being improved, so stay tuned!
Full example
We provide a backend service demo in the Github Sample repository to demonstrate this feature, please refer to os-3.0show