npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@expo-harmony/expo-battery

v55.0.13-harmony.5

Published

HarmonyOS port of expo-battery

Readme

@expo-harmony/expo-battery

GitHub 仓库 | 官方文档

为 HarmonyOS 上的 React Native 应用提供 Expo Battery 的原生实现,与官方同版本的 expo-battery 配套使用。支持读取设备电量、充电状态和低电量模式,以及订阅对应的状态变化。

安装

npm install @expo-harmony/expo-battery [email protected]

API 对照表

Hooks

useBatteryLevel()

返回 number,电量比例,取值 0 到 1。初始值为 -1,读取完成后更新,之后跟随电量事件变化。

useBatteryState()

返回 BatteryState。初始值为 BatteryState.UNKNOWN,读取完成后更新,之后跟随电池状态事件变化。

useLowPowerMode()

返回 boolean。初始值为 false,读取完成后更新,之后跟随低电量模式事件变化。

usePowerState()

返回 PowerState,包含电量、电池状态和低电量模式。三项分别读取,之后各自跟随对应事件变化。

Methods

Battery.isAvailableAsync()

返回 Promise<boolean>,恒为 true。设备是否带电池不影响该值。

Battery.getBatteryLevelAsync()

返回 Promise<number>,电量比例,取值 0 到 1。设备没有电池,或系统给出的电量不在 0 到 100 之间时返回 -1。读取失败时拒绝。

Battery.getBatteryStateAsync()

返回 Promise<BatteryState>。设备没有电池时返回 BatteryState.UNKNOWN。系统充电状态按以下方式映射:充电中为 CHARGING,未充电为 UNPLUGGED,已充满为 FULL,其他取值为 UNKNOWN。

Battery.isLowPowerModeEnabledAsync()

返回 Promise<boolean>,系统是否处于省电模式。普通省电和超级省电返回 true;API 20 起自定义省电模式也返回 true。其他电源模式返回 false。

Battery.isBatteryOptimizationEnabledAsync()

返回 Promise<boolean>,恒为 false。HarmonyOS 没有针对单个应用的电池优化开关,Android 上的这一概念在这里不适用。

Battery.getPowerStateAsync()

返回 Promise<PowerState>,一次读取电量、电池状态和低电量模式,取值分别与对应方法一致。任一项读取失败时整体拒绝。

Event Subscriptions

Battery.addBatteryLevelListener(listener)

订阅电量变化,回调收到 BatteryLevelEvent。返回 Subscription,调用 remove() 取消订阅。

只在应用处于前台时监听系统事件,切到后台会停止监听,回到前台后补发期间发生的变化。订阅后不会立即回调,只在数值变化时触发。

Battery.addBatteryStateListener(listener)

订阅电池状态变化,回调收到 BatteryStateEvent。返回 Subscription。前后台行为与电量事件一致,状态没有变化时不触发。

Battery.addLowPowerModeListener(listener)

订阅低电量模式变化,回调收到 PowerModeEvent。返回 Subscription。前后台行为与电量事件一致,模式没有变化时不触发。

Types

BatteryLevelEvent

| 属性 | 类型 | | -------------- | -------- | | batteryLevel | number |

BatteryStateEvent

| 属性 | 类型 | | -------------- | -------------- | | batteryState | BatteryState |

PowerModeEvent

| 属性 | 类型 | | -------------- | --------- | | lowPowerMode | boolean |

PowerState

| 属性 | 类型 | | -------------- | -------------- | | batteryLevel | number | | batteryState | BatteryState | | lowPowerMode | boolean |

Subscription

监听句柄,调用 remove() 取消订阅,返回 void。

Enums

BatteryState

| 成员 | 值 | 含义 | | ----------- | --- | ---------------------------------------------- | | UNKNOWN | 0 | 无法判断,设备没有电池或充电状态不在已知取值内 | | UNPLUGGED | 1 | 未充电 | | CHARGING | 2 | 充电中 | | FULL | 3 | 已充满 |

Author

expo-harmony © Baoshuo, Released under the MIT License. Authored and maintained by Baoshuo with help from contributors.

Personal Website · Blog · GitHub @renbaoshuo · Twitter @renbaoshuo