aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/api/battery_status_api
diff options
context:
space:
mode:
authorPeter Bengtsson <mail@peterbe.com>2020-12-08 14:40:17 -0500
committerPeter Bengtsson <mail@peterbe.com>2020-12-08 14:40:17 -0500
commit33058f2b292b3a581333bdfb21b8f671898c5060 (patch)
tree51c3e392513ec574331b2d3f85c394445ea803c6 /files/zh-cn/web/api/battery_status_api
parent8b66d724f7caf0157093fb09cfec8fbd0c6ad50a (diff)
downloadtranslated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.gz
translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.bz2
translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.zip
initial commit
Diffstat (limited to 'files/zh-cn/web/api/battery_status_api')
-rw-r--r--files/zh-cn/web/api/battery_status_api/index.html77
1 files changed, 77 insertions, 0 deletions
diff --git a/files/zh-cn/web/api/battery_status_api/index.html b/files/zh-cn/web/api/battery_status_api/index.html
new file mode 100644
index 0000000000..e436c0d16f
--- /dev/null
+++ b/files/zh-cn/web/api/battery_status_api/index.html
@@ -0,0 +1,77 @@
+---
+title: Battery Status API
+slug: Web/API/Battery_Status_API
+tags:
+ - API
+ - 电池
+ - 电池 API
+ - 电池状态 API
+translation_of: Web/API/Battery_Status_API
+---
+<p>{{DefaultAPISidebar("Battery API")}}</p>
+
+<p><strong>Battery Status API,</strong>更多时候被称之为 <strong>Battery API</strong>, 提供了有关系统充电级别的信息并提供了通过电池等级或者充电状态的改变提醒用户的事件。 这个可以在设备电量低的时候调整应用的资源使用状态,或者在电池用尽前保存应用中的修改以防数据丢失。</p>
+
+<p>Battery Status API 向 {{domxref("window.navigator")}} 扩展了一个 {{domxref("navigator.getBattery")}} 方法,其返回了一个battery promise, 完成后传递一个 {{domxref("BatteryManager")}} 对象,并提供了一些新的可以操作电池状态的事件。</p>
+
+<h2 id="例子">例子</h2>
+
+<p>在这个例子中,我们同时监听放电状态和电池等级和剩余事件的事件(不论是否正在充电还是在使用电池)。这可以通过监听 {{event("chargingchange")}}, {{event("levelchange")}}, {{event("chargingtimechange")}}, {{event("dischargingtimechange")}} 事件完成。</p>
+
+<pre class="brush: js">navigator.getBattery().then(function(battery) {
+
+ console.log("Battery charging? " + (battery.charging ? "Yes" : "No"));
+ console.log("Battery level: " + battery.level * 100 + "%");
+ console.log("Battery charging time: " + battery.chargingTime + " seconds");
+ console.log("Battery discharging time: " + battery.dischargingTime + " seconds");
+
+ battery.addEventListener('chargingchange', function() {
+ console.log("Battery charging? " + (battery.charging ? "Yes" : "No"));
+ });
+
+ battery.addEventListener('levelchange', function() {
+ console.log("Battery level: " + battery.level * 100 + "%");
+ });
+
+ battery.addEventListener('chargingtimechange', function() {
+ console.log("Battery charging time: " + battery.chargingTime + " seconds");
+ });
+
+ battery.addEventListener('dischargingtimechange', function() {
+ console.log("Battery discharging time: " + battery.dischargingTime + " seconds");
+ });
+
+});
+</pre>
+
+<p>另见 <a href="http://www.w3.org/TR/battery-status/#examples">标准中的例子</a>.</p>
+
+<h2 id="规范">规范</h2>
+
+<table>
+ <thead>
+ <tr>
+ <th scope="col">草案</th>
+ <th scope="col">进度</th>
+ <th scope="col">备注</th>
+ </tr>
+ </thead>
+ <tbody>
+ <tr>
+ <td>{{SpecName("Battery API")}}</td>
+ <td>{{Spec2("Battery API")}}</td>
+ <td>初步定义。</td>
+ </tr>
+ </tbody>
+</table>
+
+<h2 id="浏览器兼容性">浏览器兼容性</h2>
+
+<p>{{Compat("api.BatteryManager")}}</p>
+
+<h2 id="另见">另见</h2>
+
+<ul>
+ <li><a href="/en-US/Apps/Build/gather_and_modify_data/retrieving_battery_status_information">Retrieving battery status information - demo &amp; 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>