介绍
这个 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,是否正在充电