首页指南参考教程

Expo KeepAwake iconExpo KeepAwake

GitHub

npm

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

Android
iOS
tvOS
Web

expo-keep-awake 提供了一个 React hook 来防止屏幕休眠,以及一对函数来强制启用此行为。

¥**expo-keep-awake** provides a React hook that prevents the screen from sleeping and a pair of functions to enable this behavior imperatively.

安装

¥Installation

Terminal
npx expo install expo-keep-awake

If you're installing this in a bare React Native app, you should also follow these additional installation instructions.

用法

¥Usage

示例:hook

¥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>
  );
}

示例:functions

¥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="Activate" />
        <Button onPress={this._deactivate} title="Deactivate" />
      </View>
    );
  }

  _activate = () => {
    activateKeepAwake();
    alert('Activated!');
  };

  _deactivate = () => {
    deactivateKeepAwake();
    alert('Deactivated!');
  };
}

API

import KeepAwake from 'expo-keep-awake';

Constants

KeepAwake.ExpoKeepAwakeTag

Type: 'ExpoKeepAwakeDefaultTag'


Default tag, used when no tag has been specified in keep awake method calls.

Hooks

useKeepAwake(tag, options)

NameTypeDescription
tag
(optional)
string

Tag to lock screen sleep prevention. If not provided, the default tag is used.

Default: ExpoKeepAwakeTag
options
(optional)
KeepAwakeOptions

Additional options for the keep awake hook.


A React hook to keep the screen awake for as long as the owner component is mounted. The optionally provided tag argument is used when activating and deactivating the keep-awake feature. If unspecified, the default tag is used. See the documentation for activateKeepAwakeAsync below to learn more about the tag argument.

Returns

  • void

Methods

Deprecated. use activateKeepAwakeAsync instead.

KeepAwake.activateKeepAwake(tag)

NameTypeDescription
tag
(optional)
string

Tag to lock screen sleep prevention. If not provided, the default tag is used.

Default: ExpoKeepAwakeTag

Prevents the screen from sleeping until deactivateKeepAwake is called with the same tag value.

If the tag argument is specified, the screen will not sleep until you call deactivateKeepAwake with the same tag argument. When using multiple tags for activation you'll have to deactivate each one in order to re-enable screen sleep. If tag is unspecified, the default tag is used.

Web support is limited.

Returns

  • Promise<void>

KeepAwake.activateKeepAwakeAsync(tag)

NameTypeDescription
tag
(optional)
string

Tag to lock screen sleep prevention. If not provided, the default tag is used.

Default: ExpoKeepAwakeTag

Prevents the screen from sleeping until deactivateKeepAwake is called with the same tag value.

If the tag argument is specified, the screen will not sleep until you call deactivateKeepAwake with the same tag argument. When using multiple tags for activation you'll have to deactivate each one in order to re-enable screen sleep. If tag is unspecified, the default tag is used.

Web support is limited.

Returns

  • Promise<void>

KeepAwake.deactivateKeepAwake(tag)

NameTypeDescription
tag
(optional)
string

Tag to release the lock on screen sleep prevention. If not provided, the default tag is used.

Default: ExpoKeepAwakeTag

Releases the lock on screen-sleep prevention associated with the given tag value. If tag is unspecified, it defaults to the same default tag that activateKeepAwake uses.

Returns

  • Promise<void>

KeepAwake.isAvailableAsync()

Returns

  • Promise<boolean>

true on all platforms except unsupported web browsers.

Event Subscriptions

Only for:
Web

KeepAwake.addListener(tagOrListener, listener)

NameTypeDescription
tagOrListenerstring | KeepAwakeListener-
listener
(optional)
KeepAwakeListener-

Observe changes to the keep awake timer. On web, this changes when navigating away from the active window/tab. No-op on native.

Example

KeepAwake.addListener(({ state }) => {
  // ...
});

Returns

  • Subscription

Types

KeepAwakeEvent

NameTypeDescription
stateKeepAwakeEventState

Keep awake state.

KeepAwakeListener()

NameTypeDescription
eventKeepAwakeEvent-

KeepAwakeOptions

NameTypeDescription
listener
(optional)
KeepAwakeListenerOnly for:
Web

A callback that is invoked when the keep-awake state changes.

suppressDeactivateWarnings
(optional)
boolean

The call will throw an unhandled promise rejection on Android when the original Activity is dead or deactivated. Set the value to true for suppressing the uncaught exception.

Enums

KeepAwakeEventState

KeepAwakeEventState Values

RELEASE

KeepAwakeEventState.RELEASE = "release"
Expo 中文网 - 粤ICP备13048890号