Back to Demos List

GSW Browser: Battery Status


This page allows you to monitor the battery level of the deivce and give alert promnpts when the battery is low and critically low.



Battery


About This Demo

This demo is using the cordova-plugin-battery-status plugin. For more information click here:


    		var GSW = GSW || {};
GSW.Tests = GSW.Tests || {};

GSW.Tests.onDeviceReady = function() {
	// Cordova is now initialized. Have fun!
	console.log('Now running cordova-' + cordova.platformId + '@' + cordova.version);

	GSW.Tests.onBatteryStatus = function(status) {

		let percentage = document.querySelector('.percentage');
		let percent = document.querySelector('.percent');
		let charging = document.querySelector('.charging');
		let batteryShape = document.querySelector('.batteryShape');

		percentage.style.width = status.level + '%';
		percent.innerHTML = status.level + '%';

		if (status.isPlugged == true) {
			charging.style.color = "green";
			batteryShape.style.borderColor = "green";
			charging.innerHTML = "Status: Charging";
		} else {
			charging.style.color = "black";
			batteryShape.style.borderColor = "black";
			charging.innerHTML = "Status: Not Charging";
		}

	}


	GSW.Tests.onBatteryLow = function(status) {
		// Handle the battery low event
		alert("Battery Level Low :" + status.level + "%");
	}


	GSW.Tests.onBatteryCritical = function(status) {
		let batteryShape = document.querySelector('.batteryShape');
		batteryShape.style.borderColor = "red";
		alert("Battery Level Critical " + status.level + "%\nRecharge Soon!");
	}

	//for cordova-plugin-battery-status
	window.addEventListener("batterystatus", GSW.Tests.onBatteryStatus, false);
}
document.addEventListener('deviceready', GSW.Tests.onDeviceReady, false);