gpt4 book ai didi

android - 为手机或平板电脑充电时的能量流入

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:13:54 26 4
gpt4 key购买 nike

我想知道是否可以测量移动设备(IOS 或 Android)在充电时流过多少能量?例如,以瓦特每小时或毫安每小时为单位。

基本上,我想测量我从充电中获取了多少电量。是否有相应的 native 或低级 API?

谢谢你的帮助

最佳答案

安卓

Battery level checking Android

Battery level checking Android 1

查看演示:Battery Demo Android

iOS

是的,在 iOS 设备中,当您为设备充电时,您可以获得有关电池状态的信息。您的 batteryLevelChangedbatteryStateChanged

通知

查看演示: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;
}
}
}

关于android - 为手机或平板电脑充电时的能量流入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32581943/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com