This documentation is available as Markdown for AI agents and LLMs. See the full Markdown index or append .md to any documentation URL.

Expo TaskManager

为可以在后台运行的任务提供支持的库。

Android
iOS
tvOS
Included in Expo Go
Recommended version:
~58.0.11

expo-task-manager 提供了一个 API,使你能够管理长时间运行的任务,特别是那些可以在你的应用处于后台时运行的任务。该库的一些功能在底层被其他库使用。以下是使用 TaskManager 的 Expo SDK 库列表。

使用 Expo TaskManager 的库

🌐 Libraries using Expo TaskManager

安装

🌐 Installation

Terminal
- npx expo install expo-task-manager
- yarn expo install expo-task-manager
- pnpm expo install expo-task-manager
- bun expo install expo-task-manager

If you are installing this in an existing React Native app, make sure to install expo in your project.


配置 
iOS

🌐 Configuration 

iOS

独立应用需要一些额外的配置:在 iOS 上,每个后台功能都需要在你的 Info.plist 文件的 UIBackgroundModes 数组中添加一个特殊的键。

🌐 Standalone apps need some extra configuration: on iOS, each background feature requires a special key in UIBackgroundModes array in your Info.plist file.

阅读更多关于如何在使用 TaskManager 的每个 库 的参考中进行配置的信息。

🌐 Read more about how to configure this in the reference for each of the libraries that use TaskManager.

用法

🌐 Usage

Example
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> ); // Define the task at module scope so the background runtime can find it. // This needs to be called in the global scope, not in a React component. TaskManager.defineTask(LOCATION_TASK_NAME, ({ data, error }) => { if (error) { // Error occurred - check `error.message` for more details. return; } if (data) { const { locations } = data; // do something with the locations captured in the background } }); const styles = StyleSheet.create({ container: { flex: 1, alignItems: 'center', justifyContent: 'center', }, }); export default PermissionsButton;

应用接口

🌐 API

import * as TaskManager from 'expo-task-manager';

No API data file found, sorry!