Parameters
| Property | Type | Required | Description |
|---|---|---|---|
| success | Function | No | Callback on success |
| fail | Function | No | Callback on failure |
| complete | Function | No | Callback that always executes |
Success Callback Parameters
| Property | Type | Description |
|---|---|---|
| level | Int | The battery level of the current device |
| isCharging | Boolean | Whether the device is currently charging |
Code Example
Basic Usage
my.getBatteryInfo({
success(res) {
console.log('Battery level:', res.level);
console.log('Is charging:', res.isCharging);
}
});
Display Battery Status
Page({
data: {
batteryLevel: 0,
isCharging: false,
batteryIcon: 'battery-full'
},
onLoad() {
this.updateBatteryStatus();
},
updateBatteryStatus() {
my.getBatteryInfo({
success: (res) => {
let icon = 'battery-full';
if (res.level <= 20) icon = 'battery-empty';
else if (res.level <= 50) icon = 'battery-half';
this.setData({
batteryLevel: res.level,
isCharging: res.isCharging,
batteryIcon: res.isCharging ? 'battery-bolt' : icon
});
}
});
}
});
Low Battery Warning
Page({
onShow() {
this.checkBattery();
},
checkBattery() {
my.getBatteryInfo({
success: (res) => {
if (res.level <= 15 && !res.isCharging) {
my.alert({
title: 'Low Battery',
content: `Your battery is at ${res.level}%. Some features may be limited to save power.`
});
}
}
});
}
});
Power-Aware Features
Page({
data: {
powerSaveMode: false
},
onLoad() {
my.getBatteryInfo({
success: (res) => {
// Enable power save mode if battery is low
if (res.level <= 20 && !res.isCharging) {
this.setData({ powerSaveMode: true });
this.disableAnimations();
this.reducePollingFrequency();
}
}
});
},
disableAnimations() {
// Disable non-essential animations
},
reducePollingFrequency() {
// Reduce background refresh rate
}
});
Consider reducing resource-intensive operations when battery is low to provide a better user experience and help conserve battery life.
Related APIs
my.getSystemInfo
Get system information
my.getNetworkType
Get network type