문제

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.

도움이 되었습니까?

해결책

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>
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top