Question

I am using this event: http://cordova.apache.org/docs/en/3.0.0/cordova_events_events.md.html#batterystatus

It works fine, it gets the batterylevel when it changes or when you start/end charging.

But how do you get the batterylevel straight away, when opening the app? Thanks.

Était-ce utile?

La solution

Hi You may write one plugin to get current battery level ,more about plugin dev check this and call this plugin inside deviceready event

*javascript

 cordova.exec(function(status) {

       console.log(status); //will get current ststus

   }, function(error) {}, "Status",
                 "Status", ["givestatus"]);

this is the Native code Objective C

- (void)Status:(CDVInvokedUrlCommand*)command
{
CDVPluginResult* pluginResult = nil;
NSString* myarg = [command.arguments objectAtIndex:0];

if (myarg != nil) {

[[UIDevice currentDevice] setBatteryMonitoringEnabled:YES];
UIDevice *myDevice = [UIDevice currentDevice];
[myDevice setBatteryMonitoringEnabled:YES];
double batLeft = (float)[myDevice batteryLevel] * 100;




pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:@"%.f",batLeft]];
} else {
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR    messageAsString:@"Arg was null"];
}

[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}

add new item to config.xml also

<feature name="Status">
    <param name="ios-package" value="Status" />
</feature>
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top