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

Expo KeepAwake iconExpo KeepAwake

一个 React 组件,可防止屏幕在渲染时休眠。

Android
iOS
tvOS
Web
Included in Expo Go
Recommended version:
~58.0.3

expo-keep-awake 提供了一个 React 钩子,用于防止屏幕进入睡眠状态,并提供一对函数以命令式地启用此行为。

安装

🌐 Installation

Terminal
- npx expo install expo-keep-awake
- yarn expo install expo-keep-awake
- pnpm expo install expo-keep-awake
- bun expo install expo-keep-awake

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

用法

🌐 Usage

例子:钩子

🌐 Example: hook

Keep Awake hook
import { useKeepAwake } from 'expo-keep-awake'; import React from 'react'; import { Text, View } from 'react-native'; export default function KeepAwakeExample() { useKeepAwake(); return ( <View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}> <Text>This screen will never sleep!</Text> </View> ); }

示例:函数

🌐 Example: functions

Keep Awake functions
import { activateKeepAwake, deactivateKeepAwake } from 'expo-keep-awake'; import React from 'react'; import { Button, View } from 'react-native'; export default class KeepAwakeExample extends React.Component { render() { return ( <View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}> <Button onPress={this._activate} title="激活" /> <Button onPress={this._deactivate} title="停用" /> </View> ); } _activate = () => { activateKeepAwake(); alert('Activated!'); }; _deactivate = () => { deactivateKeepAwake(); alert('Deactivated!'); }; }

应用接口

🌐 API

import * as KeepAwake from 'expo-keep-awake';

No API data file found, sorry!