diff options
Diffstat (limited to 'files/zh-tw/web/api/battery_status_api/index.html')
-rw-r--r-- | files/zh-tw/web/api/battery_status_api/index.html | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/files/zh-tw/web/api/battery_status_api/index.html b/files/zh-tw/web/api/battery_status_api/index.html new file mode 100644 index 0000000000..cbf98d417d --- /dev/null +++ b/files/zh-tw/web/api/battery_status_api/index.html @@ -0,0 +1,38 @@ +--- +title: Battery Status API +slug: Web/API/Battery_Status_API +translation_of: Web/API/Battery_Status_API +--- +<p><strong>Battery Status API</strong> 也就是所謂的 <strong>Battery API</strong>,將提供系統電池充電容量的資訊,並在電池容量變化時送出事件,以通知使用者。此 API 可調整 Apps 的資源耗用量,在電力偏低時縮減耗電量;或可在電力耗盡之前儲存檔案,避免資料遺失。</p> +<p>Battery Status API 是以 <a href="https://developer.mozilla.org/en-US/docs/Web/API/window.navigator.battery" title="/en-US/docs/Web/API/window.navigator.battery"><code>window.navigator.battery</code></a> 屬性 (為 <a href="https://developer.mozilla.org/en-US/docs/Web/API/BatteryManager" title="/en-US/docs/Web/API/BatteryManager"><code>BatteryManager</code></a> 物件) 而擴充了 <a href="https://developer.mozilla.org/en-US/docs/Web/API/window.navigator" title="/en-US/docs/Web/API/window.navigator"><code>window.navigator</code></a>,並新增數項可讓使用者接收的新事件,以隨時監控電池狀態。</p> +<h2 id="範例">範例</h2> +<p>在此範例中,我們將分別監聽 <a href="https://developer.mozilla.org/en-US/docs/Web/Reference/Events/chargingchange" title="/en-US/docs/Web/Reference/Events/chargingchange">chargingchange</a> 與 <a href="https://developer.mozilla.org/en-US/docs/Web/Reference/Events/levelchange" title="/en-US/docs/Web/Reference/Events/levelchange">levelchange</a> 事件,而看到充電狀態 (不論是否插電進行充電) 與電池容量的變化。</p> +<pre class="brush: js">var battery = navigator.battery || navigator.mozBattery || navigator.webkitBattery; + +function updateBatteryStatus() { + console.log("Battery status: " + battery.level * 100 + " %"); + + if (battery.charging) { + console.log("Battery is charging"); + } +} + +battery.addEventListener("chargingchange", updateBatteryStatus); +battery.addEventListener("levelchange", updateBatteryStatus); +updateBatteryStatus(); +</pre> +<p>另可參閱<a href="http://dev.w3.org/2009/dap/system-info/battery-status.html#introduction">規格所提供之範例</a>。</p> +<h2 id="規格">規格</h2> +<p>{{page("/en-US/docs/Web/API/BatteryManager","Specifications")}}</p> +<h2 id="瀏覽器相容性">瀏覽器相容性</h2> +<p>{{page("/en-US/docs/Web/API/BatteryManager","Browser_compatibility")}}</p> +<h2 id="另請參閱">另請參閱</h2> +<ul> + <li><a href="http://hacks.mozilla.org/2012/02/using-the-battery-api-part-of-webapi/">部落格文章 - Using the Battery API</a></li> + <li><a href="http://davidwalsh.name/battery-api" title="http://davidwalsh.name/battery-api">David Walsh 所寫的 JavaScript Battery Api</a></li> + <li><a href="https://github.com/pstadler/battery.js" title="https://github.com/pstadler/battery.js">battery.js - 跨瀏覽器 wrapper</a></li> + <li><a href="https://developer.mozilla.org/en-US/docs/Web/API/BatteryManager" title="/en-US/docs/Web/API/BatteryManager"><code>BatteryManager</code></a></li> + <li><a href="https://developer.mozilla.org/en-US/docs/Web/API/window.navigator.battery" title="/en-US/docs/Web/API/window.navigator.battery"><code>navigator.battery</code></a></li> + <li><code><a href="http://tools.guyellisrocks.com/experiment/batterystatus" title="http://tools.guyellisrocks.com/experiment/batterystatus">測試你的瀏覽器是否支援 Battery Status API</a>。可掃 QR Code:</code></li> + <li> <a href="http://x.co/qr/batstat?s=165"><img alt="QR Code to Battery Status API Test Page" src="http://x.co/qr/batstat?s=165" style="width: 165px; height: 165px;"></a></li> +</ul> |