HTML and CSS Reference
In-Depth Information
I.3.6. Battery API: adjusting application processing based on battery life
The Battery API allows you to adjust how much processing your app does depending on
the state of the battery. In a real app, you could avoid doing any heavy processing or reduce
the number of network connections when the battery is low. In our example app, there isn't
much opportunity to cut back processing, so you're just going to draw less of Wilson as the
battery level drops. Figure I.16 shows the end result in Firefox on an Android phone.
Figure I.16. By integrating the Battery API, you can make your app do less work as the charge level drops.
The Battery API consists of four properties and four events. See the summary in table I.7 .
Table I.7. The Battery API
Property/event name
Type
Description
charging
Read-only boolean
Is the power connected?
chargingTime
Read-only double
Seconds remaining until the battery is charged.
dischargingTime
Read-only double
Seconds remaining until the battery is discharged.
A value between 0.0 and 1.0 representing the current bat-
tery charge level, where 1.0 is full.
level
Read-only double
chargingchange
Event
The value of charging has changed.
chargingtimechange
Event
The chargingTime has changed.
dischargingtimechange
Event
The dischargingTime has changed.
levelchange
Event
The level has changed.
In this example you're just going to take advantage of the charging and level prop-
erties. The following table shows the browser compatibility; this API will work on mobile
devices but also laptops.
 
 
Search WWH ::




Custom Search