1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
|
---
title: API do Estado da Bateria
slug: Web/API/Battery_Status_API
tags:
- API
- API de Bateria
- API do Estado da Bateria
- Aplicações
- Apps
- Firefox OS
- Guía
- Movel
- Resumo
- Sinopse
translation_of: Web/API/Battery_Status_API
original_slug: Web/API/API_do_Estado_da_Bateria
---
<div>{{obsolete_header}}</div>
<div>{{DefaultAPISidebar("Battery API")}}</div>
<p>A <em>API do Estado da Bateria</em>, mais conhecida como <strong>API de Bateria</strong>, fornece informação sobre o nível de carga da bateria do sistema e permite-lhe que seja notificado por eventos que são enviados quando o nível da bateria ou o estado de carregamento é alterado. Isto pode ser utilizado para ajustar a utilização de recursos da aplicação para reduzir o consumo de bateria quando a bateria estiver fraca ou para guardar as alterações antes que a bateria acabe para evitar a perda de dados.</p>
<p>The Battery Status API extends {{domxref("window.navigator")}} with a {{domxref("navigator.getBattery()")}} method returning a battery promise, which is resolved in a {{domxref("BatteryManager")}} object providing also some new events you can handle to monitor the battery status.</p>
<h2 id="Exemplo">Exemplo</h2>
<p>In this example, we watch for changes both to the charging status (whether or not we're plugged in and charging) and for changes to the battery level and timing. This is done by listening for the {{event("chargingchange")}}, {{event("levelchange")}}, {{event("chargingtimechange")}}, {{event("dischargingtimechange")}} events.</p>
<pre class="brush: js">navigator.getBattery().then(function(battery) {
function updateAllBatteryInfo(){
updateChargeInfo();
updateLevelInfo();
updateChargingInfo();
updateDischargingInfo();
}
updateAllBatteryInfo();
battery.addEventListener('chargingchange', function(){
updateChargeInfo();
});
function updateChargeInfo(){
console.log("Battery charging? "
+ (battery.charging ? "Yes" : "No"));
}
battery.addEventListener('levelchange', function(){
updateLevelInfo();
});
function updateLevelInfo(){
console.log("Battery level: "
+ battery.level * 100 + "%");
}
battery.addEventListener('chargingtimechange', function(){
updateChargingInfo();
});
function updateChargingInfo(){
console.log("Battery charging time: "
+ battery.chargingTime + " seconds");
}
battery.addEventListener('dischargingtimechange', function(){
updateDischargingInfo();
});
function updateDischargingInfo(){
console.log("Battery discharging time: "
+ battery.dischargingTime + " seconds");
}
});
</pre>
<p>See also <a class="external" href="http://www.w3.org/TR/battery-status/#examples">the example in the specification</a>.</p>
<h2 id="Especificações">Especificações</h2>
<table class="standard-table">
<thead>
<tr>
<th scope="col">Especificação</th>
<th scope="col">Estado</th>
<th scope="col">Comentário</th>
</tr>
</thead>
<tbody>
<tr>
<td>{{SpecName("Battery API")}}</td>
<td>{{Spec2("Battery API")}}</td>
<td>Initial definition</td>
</tr>
</tbody>
</table>
<h2 id="Compatibilidade_do_navegador">Compatibilidade do navegador</h2>
<p>{{CompatibilityTable}}</p>
<div id="compat-desktop">
<table class="compat-table">
<tbody>
<tr>
<th>Feature</th>
<th>Chrome</th>
<th>Firefox (Gecko)</th>
<th>Internet Explorer</th>
<th>Opera</th>
<th>Safari</th>
</tr>
<tr>
<td>Basic support</td>
<td>{{CompatChrome(39.0)}}</td>
<td>{{CompatGeckoDesktop("10")}} {{property_prefix("moz")}}<br>
{{CompatGeckoDesktop("16")}}<sup>[1]</sup><br>
{{CompatGeckoDesktop("43")}}<sup>[3]</sup><br>
{{CompatGeckoDesktop("52")}}<sup>[4]</sup></td>
<td>{{CompatNo}}</td>
<td>25</td>
<td>{{CompatNo}}</td>
</tr>
</tbody>
</table>
</div>
<div id="compat-mobile">
<table class="compat-table">
<tbody>
<tr>
<th>Feature</th>
<th>Android</th>
<th>Android Webview</th>
<th>Firefox Mobile (Gecko)</th>
<th>IE Mobile</th>
<th>Opera Mobile</th>
<th>Safari Mobile</th>
<th>Chrome for Android</th>
</tr>
<tr>
<td>Basic support</td>
<td>{{CompatNo}}</td>
<td>{{CompatChrome(40.0)}}</td>
<td>
<p>{{CompatGeckoMobile("10")}} {{property_prefix("moz")}}<br>
{{CompatGeckoMobile("16")}}<sup>[1]</sup><br>
{{CompatGeckoMobile("43")}}<sup>[3]</sup><br>
{{CompatGeckoMobile("52")}}<sup>[3]</sup></p>
</td>
<td>{{CompatNo}}</td>
<td>25<sup>[2]</sup></td>
<td>{{CompatNo}}</td>
<td>{{CompatChrome(42.0)}}<sup>[2]</sup></td>
</tr>
</tbody>
</table>
</div>
<p>[1] Disabled by default in Firefox 10.0, but can be enabled setting the preference <code>dom.battery.enabled</code> to <code>true</code>. Starting with Firefox 11.0, <code>mozBattery</code> is enabled by default. The Battery API is currently supported on Android, Windows, and Linux with UPower installed. Support for MacOS is available starting with Gecko 18.0 {{geckoRelease("18.0")}}. Firefox also provides support for the deprecated {{domxref("navigator.battery")}}.</p>
<p>[2] Values for {{domxref("BatteryManager.chargingTime")}} and {{domxref("BatteryManager.dischargingTime")}} are always equal to <code>Infinity</code>.</p>
<p>[3] The new promise-based syntax for {{domxref("Navigator.getBattery()")}} is supported from Firefox 43 onwards.</p>
<p>[4] From Firefox 52 onwards, the Battery Status API is only available in chrome/privileged code.</p>
<h2 id="Consultar_também">Consultar também</h2>
<ul>
<li><a href="/en-US/Apps/Build/gather_and_modify_data/retrieving_battery_status_information">Retrieving battery status information - demo & article</a></li>
<li><a class="external" href="http://hacks.mozilla.org/2012/02/using-the-battery-api-part-of-webapi/">Hacks blog post - Using the Battery API</a></li>
</ul>
|