I have a phonegap-bootstrap project at GitHub -- a demo application, written in PhoneGap 2.9. I've placed event handlers for every event supported by PhoneGap. All of them fires without any problems, except for battery-related events.

I was more then sure, that this is because of missing permission, but it turned out to be wrong asumption.

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, beyond my imagination, PhoneGap Build application isn't firing battery-related events. And only those battery-related.

Battery-related events never gets fired and code inside "global" (general) battery handler (batteryHandler) isn't ever executed, so lblBatteryLevel and lblBatteryPlugged divs remains in their initial values ("[waiting]").

And all other event handlers are coded exactly in 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 defined as log-writing events only:

/**
 * 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 fires, except for battery-related ones.

I was more then sure, that this is because of missing permission, but I triple checked, that line <feature name="http://api.phonegap.com/1.0/battery"/> is placed in my config.xml file.

I tried asking guys at PhoneGap Build Get Satisfaction forum, but got no advice, what can be wrong.

I have created a jsFiddle, where HTML part contains my entire config.xml file and Javascript part contains most parts out of my Javascript code, those related to battery. Maybe someone have some time, reviewing it. I can't find any mistakes there and the battery-related code is still not working.

有帮助吗?

解决方案 3

My phonegap-battery repo at GitHub contains battery demo application, which seems to be working.

It looks, that I've solved the battery problem, though I don't recall right now, what I did to solve it.

I only recall, that with some help from PhoneGap Build community / PGB GetSatisfaction forum, I managed to find what was wrong and fixed battery demo application to be fully operable.

其他提示

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

Add battery status plugin to your project.

Open command prompt

cd your_project_dir

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

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top