This is documentation for the next SDK version. For up-to-date documentation, see the latest version (SDK 55).
Expo 电池
一个为物理设备提供电池信息以及相应事件监听器的库。
For the complete documentation index, see llms.txt. Use this Use this file to discover all available pages.
expo-battery 为物理设备提供电池信息(例如电量水平、设备是否正在充电等),以及相应的事件监听器。
安装
- npx expo install expo-batteryIf you are installing this in an existing React Native app, make sure to install expo in your project.
用法
import { useBatteryLevel } from 'expo-battery'; import { StyleSheet, Text, View } from 'react-native'; export default function App() { const batteryLevel = useBatteryLevel(); return ( <View style={styles.container}> <Text>当前电池电量:{batteryLevel}</Text> </View> ); } const styles = StyleSheet.create({ container: { flex: 1, marginTop: 15, alignItems: 'center', justifyContent: 'center', }, });
API
import * as Battery from 'expo-battery';
Hooks
Gets the device's battery level, as in getBatteryLevelAsync.
numberThe battery level of the device.
Example
const batteryLevel = useBatteryLevel();
Gets the device's battery state, as in getBatteryStateAsync.
BatteryStateThe battery state of the device.
Example
const batteryState = useBatteryState();
Boolean that indicates if the device is in low power or power saver mode, as in isLowPowerModeEnabledAsync.
booleanReturns a boolean indicating if the device is in low power mode.
Example
const lowPowerMode = useLowPowerMode();
Gets the device's power state information, as in getPowerStateAsync.
PowerStateReturns power state information.
Example
const { lowPowerMode, batteryLevel, batteryState } = usePowerState();
Methods
Gets the battery level of the device as a number between 0 and 1, inclusive. If the device
does not support retrieving the battery level, this method returns -1. On web, this method
always returns 1.
Promise<number>A Promise that fulfils with a number between 0 and 1 representing the battery level,
or -1 if the device does not provide it.
Example
await Battery.getBatteryLevelAsync(); // 0.759999
Tells the battery's current state. On web, this always returns BatteryState.UNKNOWN.
Promise<BatteryState>Returns a Promise which fulfills with a Battery.BatteryState enum
value for whether the device is any of the five states.
Example
await Battery.getBatteryStateAsync(); // BatteryState.CHARGING
Gets the power state of the device including the battery level, whether it is plugged in, and if the system is currently operating in Power Saver Mode (Android) or Low Power Mode (iOS). This method re-throws any errors that occur when retrieving any of the power-state information.
Promise<PowerState>Returns a Promise which fulfills with PowerState object.
Example
await Battery.getPowerStateAsync(); // { // batteryLevel: 0.759999, // batteryState: BatteryState.UNPLUGGED, // lowPowerMode: true, // }
Resolves with whether the battery API is available on the current device. The value of this
property is true on Android and physical iOS devices and false on iOS simulators. On web,
it depends on whether the browser supports the web battery API.
Promise<boolean>Checks whether battery optimization is enabled for your application. If battery optimization is enabled for your app, background tasks might be affected when your app goes into doze mode state. (only on Android 6.0 or later)
Promise<boolean>Returns a Promise which fulfills with a boolean value of either true or false,
indicating whether the battery optimization is enabled or disabled, respectively. (Android only)
Example
await Battery.isBatteryOptimizationEnabledAsync(); // true
Gets the current status of Power Saver mode on Android and Low Power mode on iOS. If a platform
doesn't support Low Power mode reporting (like web, older Android devices), the reported low-power
state is always false, even if the device is actually in low-power mode.
Promise<boolean>Returns a Promise which fulfills with a boolean value of either true or false,
indicating whether low power mode is enabled or disabled.
Example
Power Saver Mode (Android) or Low Power Mode (iOS) are enabled.
await Battery.isLowPowerModeEnabledAsync(); // true
Event Subscriptions
| Parameter | Type | Description |
|---|---|---|
| listener | (event: BatteryLevelEvent) => void | A callback that is invoked when battery level changes. The callback is provided a
single argument that is an object with a |
Subscribe to the battery level change updates.
On Android devices, the event fires only when significant changes happens, which is when the
battery level drops below android.intent.action.BATTERY_LOW
or rises above android.intent.action.BATTERY_OKAY
from a low battery level. See Monitor the Battery Level and Charging State
in Android documentation for more information.
On iOS devices, the event fires when the battery level drops one percent or more, but is only fired once per minute at maximum.
On web, the event never fires.
EventSubscriptionA Subscription object on which you can call remove() to unsubscribe from the listener.
| Parameter | Type | Description |
|---|---|---|
| listener | (event: BatteryStateEvent) => void | A callback that is invoked when battery state changes. The callback is provided a
single argument that is an object with a |
Subscribe to the battery state change updates to receive an object with a Battery.BatteryState
enum value for whether the device is any of the five states.
On web, the event never fires.
EventSubscriptionA Subscription object on which you can call remove() to unsubscribe from the listener.
| Parameter | Type | Description |
|---|---|---|
| listener | (event: PowerModeEvent) => void | A callback that is invoked when Power Saver Mode (Android) or Low Power Mode (iOS)
changes. The callback is provided a single argument that is an object with a |
Subscribe to Power Saver Mode (Android) or Low Power Mode (iOS) updates. The event fires whenever the power mode is toggled.
On web, the event never fires.
EventSubscriptionA Subscription object on which you can call remove() to unsubscribe from the listener.
Interfaces
A subscription object that allows to conveniently remove an event listener from the emitter.
Types
| Property | Type | Description |
|---|---|---|
| batteryLevel | number | A number between |
| Property | Type | Description |
|---|---|---|
| batteryState | BatteryState | An enum value representing the battery state. |
| Property | Type | Description |
|---|---|---|
| lowPowerMode | boolean | A boolean value, |
| Property | Type | Description |
|---|---|---|
| batteryLevel | number | A number between |
| batteryState | BatteryState | An enum value representing the battery state. |
| lowPowerMode | boolean | A boolean value, |
Enums
BatteryState.UNPLUGGED = 1If the battery is discharging (typically not connected to power). On Android, this
corresponds to BATTERY_STATUS_DISCHARGING.
BatteryState.NOT_CHARGING = 4The battery is not charging while power is connected (AC/USB/wireless), for
example when battery protection limits charge to 80%, or optimized charging pauses. This
differs from UNPLUGGED (discharging on battery). On iOS and web, this value is never returned.