Cordova batterystatus - 如何立即获得水平?

时间:2014-03-31 10:29:59

标签: ios iphone objective-c cordova batterylevel

我正在使用此活动: http://cordova.apache.org/docs/en/3.0.0/cordova_events_events.md.html#batterystatus

它工作正常,当它改变或开始/结束充电时它会得到电池。

但是,打开应用程序时,如何直接获得batterylevel?感谢。

1 个答案:

答案 0 :(得分:1)

您好您可以编写一个插件来获取当前的电池电量,更多关于插件开发检查this并在 deviceready 事件中调用此插件

*的 的javascript

 cordova.exec(function(status) {

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

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

这是原生代码目标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];
}

将新项添加到config.xml

<feature name="Status">
    <param name="ios-package" value="Status" />
</feature>