Skip to content

Latest commit

 

History

History
98 lines (70 loc) · 3.04 KB

README.md

File metadata and controls

98 lines (70 loc) · 3.04 KB
title description
Battery Status
Get events for device battery level.

phonegap-plugin-battery-status

This plugin provides an implementation based on the W3C Battery Status Events API. The method navigator.getBattery() returns a promise with a BatteryManager object which has the following event handlers:

  • onchargingchange
  • onchargingtimechange
  • ondischargingtimechange
  • onlevelchange

Installation

phonegap plugin add phonegap-plugin-battery-status

Status object

The BatteryManager object has the following properties:

  • level: The battery charge percentage (0-100). (Number)
  • charging: A boolean that indicates whether the device is plugged in. (Boolean)
  • chargingTime: The time remaining to fully charge the battery. (Number)
  • dischargingTime: The time remaining for the battery level to come down to 0. (Number)

onchargingchange event

Fires when the device is plugged in or unplugged.

Example

    navigator.getBattery().then(function(battery) {
        battery.onchargingchange = function() {
            console.log(this.level);
        };
    });

onchargingtimechange event

Fires when the time required to charge the device changes.

Example

    navigator.getBattery().then(function(battery) {
        console.log(battery.level);
        battery.onchargingtimechange = function() {
            console.log(this.level);
        };
    });

ondischargingtimechange event

Fires when the time required to discharge the device changes.

Example

    navigator.getBattery().then(function(battery) {
        battery.ondischargingtimechange = function() {
            console.log(this.level);
        };
    });

onlevelchange event

Fires when the battery level of the device changes.

Example

    navigator.getBattery().then(function(battery) {
        battery.onlevelchange = function() {
        console.log(this.level);
        };
    });

Quirks: The onchargingtimechange and the ondischargingtimechange events and the chargingtime and dischargingTime properties are not supported on iOS.