Battery
Battery reports the level, charging state and power-saving mode, for the work an app should not
be doing on 5%.
Import
import { Battery } from '@ng-native/expo';
Inject
inject(Battery)Install
npx expo install expo-batteryimport { Battery } from '@ng-native/expo/battery';The smallest useful example
import { Component, inject } from '@angular/core';
import { Text } from '@ng-native/components';
import { Battery } from '@ng-native/expo/battery';
@Component({
selector: 'app-status',
imports: [Text],
template: `
@if (battery.low()) {
<text>Battery saver recommended</text>
}
`,
})
export class Status {
protected readonly battery = inject(Battery);
}What it reports
level- nought to one. Starts at1until the platform answers, since an app should not open dimmed.known- whether the platform will actually say. False on a simulator, and on a device that reports a level of-1: iOS answers-1for "no battery to report on", andleveltreats that as1rather than passing a negative number through as a real reading that happens to be below every threshold an app has.state-'unknown','unplugged','charging'or'full'. Android'sNOT_CHARGING(plugged in and holding) reads as'full': for every purpose an app has, the battery is not going down.charging- true whenstateis'charging'or'full'.saving- Low Power Mode on iOS, Battery Saver on Android. Startsfalse.low- under a fifth and not charging: the point at which an app should start doing less. False whenever the level is unknown, since "I cannot tell" and "almost flat" are not the same answer and only one of them is a reason to degrade.
Without the module installed
level reads 1, known is false, state is 'unknown', saving and low are false.
Nothing throws.
Reference
Batteryservice@ng-native/expoimport { Battery } from '@ng-native/expo';Signals
levelSignal<number>Nought to one. One until the platform says otherwise: an app should not open dimmed.
knownSignal<boolean>Whether the platform will say. False on a simulator, and on a device that answers -1.
stateSignal<BatteryState>savingSignal<boolean>Low Power Mode on iOS, Battery Saver on Android. The signal to skip the background refresh, drop the frame rate of a decoration, or stop prefetching.
chargingSignal<boolean>lowSignal<boolean>Under a fifth and not charging: the point at which an app should start doing less.