介紹
這個 Cordova 插件用於監視設備的電池狀態。
安裝
cordova plugin add cordova-plugin-battery-status
使用方法:
應用程序可以使用window.addeventlistener將上述事件的事件偵聽器,不過要在deviceready事件之後觸發
document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady() {
console.log(“在此處使用插件”);
}
事件
這個插件提供了一箇舊版本的實現電池狀態事件API。它增加了以下三個事件在window 對象:
· batterystatus
· batterylow
· batterycritical
狀態對象Status object
在這個插件中的所有事件還具有以下屬性的對象:
level:電池的充電率(0-100)。(數字型)
isPlugged:用一個布爾值指示是否這個設備已經插入電源。(布爾值)
電池狀態事件batterystatus event
當電池電量百分比變化至少1%,或當設備插入或拔出。返回一個電池狀態的對象
支持的平臺Supported Platforms
· Amazon Fire OS
· iOS
· Android
· BlackBerry 10
· Windows Phone 7 and 8
· Windows (Windows Phone 8.1 and Windows 10)
· Firefox OS
· Browser (Chrome, Firefox, Opera)
Android & Amazon Fire OS 特性:
警告:Android和Fire OS的實現是貪婪的,長時間使用會消耗設備的電池。
Windows Phone 7 & Windows Phone 8 特性:
這個level屬性不支持Windows Phone 7因爲操作系統不提供原生API來確定電池電量。
這個isPlugged 參數是支持的.
Windows Phone 8.1 特性:
這個isPlugged參數不支持Windows Phone 8.1。
這個level參數是支持.
蓄電池事件batterylow event
當電池電量百分比達到了較低的閾值時,觸發此事件。這個值是特定於設備的(此值在不同的設備可能有所不同)。返回一個目標含電池狀態
支持的平臺Supported Platforms
· Amazon Fire OS
· iOS
· Android
· BlackBerry 10
· Firefox OS
· Windows (Windows Phone 8.1 and Windows 10)
· Browser (Chrome, Firefox, Opera)
Windows Phone 8.1 特性:
這個蓄電池事件會在Windows Phone 8.1不管設備插入或不。這是因爲操作系統沒有提供API來檢測設備是否插好。
batterycritical事件
當電池充電率達到臨界電荷閾值,觸發此事件。這個值是特定於設備的(此值在不同的設備可能有所不同)。返回一個含電池狀態的對象
支持的平臺Supported Platforms
· Amazon Fire OS
· iOS
· Android
· BlackBerry 10
· Firefox OS
· Windows (Windows Phone 8.1 and Windows 10)
· Browser (Chrome, Firefox, Opera)
Windows Phone 8.1 特性:
這個batterycritical事件會在Windows Phone 8.1不管設備是否插入。這是因爲操作系統沒有提供API來檢測設備是否插好。
示例
index.html:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Security-Policy" content="default-src 'self' data: gap: https://ssl.gstatic.com 'unsafe-eval'; style-src 'self' 'unsafe-inline'; media-src *; img-src 'self' data: content:;">
<meta name="format-detection" content="telephone=no">
<meta name="msapplication-tap-highlight" content="no">
<meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width">
<title>Hello World</title>
</head>
<body>
<div class="app">
<h1>電池狀態</h1>
<div id="device"></div>
</div>
<script type="text/javascript" src="cordova.js"></script>
<script type="text/javascript" src="js/index.js"></script>
</body>
</html>
index.js:
var app = {
initialize: function() {
document.addEventListener('deviceready', this.onDeviceReady.bind(this), false);
},
onDeviceReady: function() {
this.onBatterystatu();
this.onBatterylow();
this.onBatterycritical();
},
/* 電量變化 */
onBatterystatu: function(){
window.addEventListener("batterystatus", onBatteryStatus, false);
function onBatteryStatus(status) {
console.log("Level: " + status.level + "///// isPlugged: " + status.isPlugged);
}
},
/* 電量較低 */
onBatterylow:function(){
window.addEventListener("batterylow", onBatteryLow, false);
function onBatteryLow(status) {
console.log("Battery Level Low " + status.level + "%");
}
},
/* 電量已滿 */
onBatterycritical: function(){
window.addEventListener("batterycritical", onBatteryCritical, false);
function onBatteryCritical(status) {
console.log("Battery Level Critical " + status.level + "%\nRecharge Soon!");
}
}
};
app.initialize();
運行:
顯示電量100,是否正在充電