PhoneGap Build app does not trigger battery related events

I have a phonegap-bootstrap project on GitHub , a demo application written in PhoneGap 2.9. I placed event handlers for each event supported by PhoneGap. All of them work without any problems, except in cases related to the battery.

I was more than sure that this was due to a lack of permission, but it turned out to be wrong.

So ... Can someone tell me what is wrong with the following code:

 var app = { init: function() { document.addEventListener('deviceready', app.deviceReadyHandler, false); app.writeEventLog('app.init();'); }, deviceReadyHandler: function() { document.addEventListener('batterylow', app.batteryLowHandler, false); document.addEventListener('batterystatus', app.batteryStatusHandler, false); document.addEventListener('batterycritical', app.batteryCriticalHandler, false); }, batteryLowHandler: function(info){app.batteryHandler('low', info);}, batteryStatusHandler: function(info){app.batteryHandler('status', info);}, batteryCriticalHandler: function(info){app.batteryHandler('critical', info);}, batteryHandler: function(type, info) { $('#lblBatteryLevel').html(info.level + '%'); $('#lblBatteryPlugged').html((info.isPlugged) ? 'yes' : 'no'); app.writeEventLog('app.battery' + event + 'Handler(level = ' + info.level +', isPlugged = ' + info.isPlugged + ');'); } } app.init(); 

For some reason, apart from my imagination, the PhoneGap Build app does not fire battery events. And only those related to the battery.

Battery events never fire, and the code inside the "global" ( batteryHandler ) never runs, so lblBatteryLevel and lblBatteryPlugged divs remain at their initial values ​​("[wait]").

And all other event handlers are encoded in exactly the same way. Declared in deviceReadyHandler :

 document.addEventListener('pause', app.pauseHandler, false); document.addEventListener('resume', app.resumeHandler, false); document.addEventListener('online', app.onlineHandler, false); document.addEventListener('offline', app.offlineHandler, false); document.addEventListener('menubutton', app.menuButtonHandler, false); document.addEventListener('searchbutton', app.searchButtonHandler, false); 

And then only logging events are determined:

 /** * Demo purpose only events. */ pauseHandler: function(){app.writeEventLog('app.pauseHandler();');}, resumeHandler: function(){app.writeEventLog('app.resumeHandler();');}, onlineHandler: function(){app.writeEventLog('app.onlineHandler();');}, offlineHandler: function(){app.writeEventLog('app.offlineHandler();');}, menuButtonHandler: function(){app.writeEventLog('app.menuButtonHandler();');}, searchButtonHandler: function(){app.writeEventLog('app.searchButtonHandler();');}, 

All of them work, except for those associated with the battery.

I was more than sure that this was due to a lack of permission, but I checked three times that the line <feature name="http://api.phonegap.com/1.0/battery"/> fits in my config.xml file.

I tried asking guys in the PhoneGap Build Get Satisfaction forum, but didn’t get any advice what might be wrong.

I created jsFiddle where the HTML part contains my entire config.xml file and the Javascript part contains most of the battery related Javascript parts. Maybe someone has time reviewing it. I cannot find errors there, and the battery related code still does not work.

+4
source share
3 answers

My phonegap-battery repo on GitHub contains a demo battery application that works .

It seems that I solved the battery problem, although now I do not remember what I did to solve it.

I just remember that with some help from the PhoneGap Build / PGB GetSatisfaction community, I managed to find what was wrong, and the battery demo app was fully functional.

0
source

Put everything inside the deviceReadyHandler function. I think battery events are triggered even before the device is ready.

0
source

Add the battery status plugin to your project.

Open command prompt

cd your_project_dir

local phonegap plugin add org.apache.cordova.battery-status

0
source

Source: https://habr.com/ru/post/1492646/


All Articles