This is documentation for the next SDK version. For up-to-date documentation, see the latest version (SDK 55).
Expo TaskManager
一个为可在后台运行的任务提供支持的库。
For the complete documentation index, see llms.txt. Use this Use this file to discover all available pages.
expo-task-manager 提供了一个 API,允许你管理长时间运行的任务,尤其是那些可以在应用处于后台时运行的任务。这个库的一些功能会被其他库在底层使用。以下是使用 TaskManager 的 Expo SDK 库列表。
使用 Expo TaskManager 的库
安装
- npx expo install expo-task-managerIf you are installing this in an existing React Native app, make sure to install expo in your project.
配置 iOS
独立应用需要一些额外配置:在 iOS 上,每个后台功能都需要在你的 Info.plist 文件中的 UIBackgroundModes 数组里添加一个特殊的键。
关于如何进行配置,请阅读各个使用 TaskManager 的库的参考文档。
示例
import React from 'react'; import { Button, View, StyleSheet } from 'react-native'; import * as TaskManager from 'expo-task-manager'; import * as Location from 'expo-location'; const LOCATION_TASK_NAME = 'background-location-task'; const requestPermissions = async () => { const { status: foregroundStatus } = await Location.requestForegroundPermissionsAsync(); if (foregroundStatus === 'granted') { const { status: backgroundStatus } = await Location.requestBackgroundPermissionsAsync(); if (backgroundStatus === 'granted') { await Location.startLocationUpdatesAsync(LOCATION_TASK_NAME, { accuracy: Location.Accuracy.Balanced, }); } } }; const PermissionsButton = () => ( <View style={styles.container}> <Button onPress={requestPermissions} title="启用后台定位" /> </View> ); TaskManager.defineTask(LOCATION_TASK_NAME, ({ data, error }) => { if (error) { // 发生错误 - 请检查 `error.message` 以获取更多详情。 return; } if (data) { const { locations } = data; // 在后台捕获到的位置上执行一些操作 } }); const styles = StyleSheet.create({ container: { flex: 1, alignItems: 'center', justifyContent: 'center', }, }); export default PermissionsButton;
API
import * as TaskManager from 'expo-task-manager';
Methods
| Parameter | Type | Description |
|---|---|---|
| taskName | string | Name of the task. It must be the same as the name you provided when registering the task. |
| taskExecutor | TaskManagerTaskExecutor<T> | A function that will be invoked when the task with given |
Defines task function. It must be called in the global scope of your JavaScript bundle.
In particular, it cannot be called in any of React lifecycle methods like componentDidMount.
This limitation is due to the fact that when the application is launched in the background,
we need to spin up your JavaScript app, run your task and then shut down — no views are mounted
in this scenario.
voidProvides information about tasks registered in the app.
Promise<TaskManagerTask[]>A promise which fulfills with an array of tasks registered in the app.
Example
[ { taskName: 'location-updates-task-name', taskType: 'location', options: { accuracy: Location.Accuracy.High, showsBackgroundLocationIndicator: false, }, }, { taskName: 'geofencing-task-name', taskType: 'geofencing', options: { regions: [...], }, }, ]
| Parameter | Type | Description |
|---|---|---|
| taskName | string | Name of the task. |
Retrieves options associated with the task, that were passed to the function registering the task
(e.g. Location.startLocationUpdatesAsync).
Promise<TaskOptions>A promise which fulfills with the options object that was passed while registering task
with given name or null if task couldn't be found.
Determine if the TaskManager API can be used in this app.
Promise<boolean>A promise which fulfills with true if the API can be used, and false otherwise.
With Expo Go, TaskManager is not available on Android, and does not support background execution on iOS.
Use a development build to avoid limitations: https://expo.fyi/dev-client.
On the web, it always returns false.
| Parameter | Type | Description |
|---|---|---|
| taskName | string | Name of the task. |
Checks whether the task is already defined.
boolean| Parameter | Type | Description |
|---|---|---|
| taskName | string | Name of the task. |
Determine whether the task is registered. Registered tasks are stored in a persistent storage and preserved between sessions.
Promise<boolean>A promise which resolves to true if a task with the given name is registered, otherwise false.
Unregisters all tasks registered for the running app. You may want to call this when the user is signing out and you no longer need to track his location or run any other background tasks.
Promise<void>A promise which fulfills as soon as all tasks are completely unregistered.
| Parameter | Type | Description |
|---|---|---|
| taskName | string | Name of the task to unregister. |
Unregisters task from the app, so the app will not be receiving updates for that task anymore.
It is recommended to use methods specialized by modules that registered the task, eg.
Location.stopLocationUpdatesAsync.
Promise<void>A promise which fulfills as soon as the task is unregistered.
Interfaces
Error object that can be received through TaskManagerTaskBody when the
task fails.
| Property | Type | Description |
|---|---|---|
| code | string | number | - |
| message | string | - |
Represents an already registered task.
| Property | Type | Description |
|---|---|---|
| options | any | Provides |
| taskName | string | Name that the task is registered with. |
| taskType | string | Type of the task which depends on how the task was registered. |
Represents the object that is passed to the task executor.
| Property | Type | Description |
|---|---|---|
| data | T | An object of data passed to the task executor. Its properties depend on the type of the task. |
| error | TaskManagerError | null | Error object if the task failed or |
| executionInfo | TaskManagerTaskBodyExecutionInfo | Additional details containing unique ID of task event and name of the task. |
Additional details about execution provided in TaskManagerTaskBody.
| Property | Type | Description |
|---|---|---|
| appState(optional) | 'active' | 'background' | 'inactive' | Only for: iOS State of the application. |
| eventId | string | Unique ID of task event. |
| taskName | string | Name of the task. |
Types
Type of task executor – a function that handles the task.
| Parameter | Type |
|---|---|
| body | TaskManagerTaskBody<T> |
Promise<any>