This documentation is available as Markdown for AI agents and LLMs. See the full Markdown index or append .md to any documentation URL.
This is documentation for the next SDK version. For up-to-date documentation, see the latest version (SDK 57).
Expo 任务管理器
一个为可在后台运行的任务提供支持的库。
expo-task-manager 提供了一个 API,允许你管理长时间运行的任务,尤其是那些可以在应用处于后台时运行的任务。这个库的一些功能会被其他库在底层使用。以下是使用 TaskManager 的 Expo SDK 库列表。
使用 Expo TaskManager 的库
安装
If you are installing this in an existing React Native app, make sure to install expo in your project.
你可以在 Expo Go 应用中测试
TaskManager。不过,请查看每个使用TaskManager的库的文档,以确认它是否支持在 Expo Go 中测试。
配置 iOS
独立应用需要一些额外配置:在 iOS 上,每个后台功能都需要在你的 Info.plist 文件中的 UIBackgroundModes 数组里添加一个特殊的键。
关于如何进行配置,请阅读各个使用 TaskManager 的库的参考文档。
用法
注意:
TaskManager.defineTask必须在模块作用域中调用,而不是在 React 组件内部。上面的示例将其放在屏幕文件的模块作用域中,这对非路由应用有效。在 Expo Router 项目中,请将其移到单独的文件中(例如 tasks.ts),并在根布局(app/_layout.tsx)顶部导入该文件,这样在任何导航运行之前就会注册该任务。
API
import * as TaskManager from 'expo-task-manager';
Methods
Defines task function. It must be called in the global scope of your JavaScript bundle, not inside a React component. When the application is launched in the background, no components are mounted, so the task must already be registered at module load time.
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: [...], }, }, ]
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.
Checks whether the task is already defined.
booleanDetermine 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.
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.
Additional details about execution provided in TaskManagerTaskBody.