我正在使用此活动: http://cordova.apache.org/docs/en/3.0.0/cordova_events_events.md.html#batterystatus
它工作正常,当它改变或开始/结束充电时它会得到电池。
但是,打开应用程序时,如何直接获得batterylevel?感谢。
答案 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>