【发布时间】:2015-09-15 09:03:25
【问题描述】:
我想知道是否可以测量移动设备(IOS 或 Android)充电时的电量? 以瓦特/小时或毫安/小时为例。
基本上我想测量我从充电中消耗了多少电量。 是否有用于此的本机或低级 API?
感谢您的帮助
【问题讨论】:
标签: android ios mobile flow energy
我想知道是否可以测量移动设备(IOS 或 Android)充电时的电量? 以瓦特/小时或毫安/小时为例。
基本上我想测量我从充电中消耗了多少电量。 是否有用于此的本机或低级 API?
感谢您的帮助
【问题讨论】:
标签: android ios mobile flow energy
安卓
Battery level checking Android
Battery level checking Android 1
查看演示:Battery Demo Android
iOS
是的,在 iOS 设备中,当您为设备充电时,您可以获得有关电池状态的信息。通知您的batteryLevelChanged 和batteryStateChanged
查看演示:Batterry Demo iOS
注意:在 iOS 设备上运行此演示。不是模拟器。
// Register for battery level and state change notifications.
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(batteryLevelChanged:)
name:UIDeviceBatteryLevelDidChangeNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(batteryStateChanged:)
name:UIDeviceBatteryStateDidChangeNotification object:nil];
updateBatteryLevel 的代码:
- (void)updateBatteryLevel
{
float batteryLevel = [UIDevice currentDevice].batteryLevel;
if (batteryLevel < 0.0) {
// -1.0 means battery state is UIDeviceBatteryStateUnknown
self.levelLabel.text = NSLocalizedString(@"Unknown", @"");
}
else {
static NSNumberFormatter *numberFormatter = nil;
if (numberFormatter == nil) {
numberFormatter = [[NSNumberFormatter alloc] init];
[numberFormatter setNumberStyle:NSNumberFormatterPercentStyle];
[numberFormatter setMaximumFractionDigits:1];
}
NSNumber *levelObj = [NSNumber numberWithFloat:batteryLevel];
self.levelLabel.text = [numberFormatter stringFromNumber:levelObj];
}
}
- (void)updateBatteryState
{
NSArray *batteryStateCells = @[self.unknownCell, self.unpluggedCell, self.chargingCell, self.fullCell];
UIDeviceBatteryState currentState = [UIDevice currentDevice].batteryState;
for (int i = 0; i < [batteryStateCells count]; i++) {
UITableViewCell *cell = (UITableViewCell *) batteryStateCells[i];
if (i + UIDeviceBatteryStateUnknown == currentState) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else {
cell.accessoryType = UITableViewCellAccessoryNone;
}
}
}
【讨论】:
最简单(也是最准确)的解决方案之一是使用电流表来记录通过墙上插头的电流。任何功率测量 API 都取决于板上的仪器。有时是直接测量(准确),有时是计算/推断(可能准确,也可能非常不准确)。
这些数据记录器的范围从工业(更昂贵,工作量更少)到 DIY(便宜,工作量更大)。你想要什么取决于你有多少时间以及你打算使用它的频率。
【讨论】: