gpt4 book ai didi

iphone - 充电完成前的剩余时间,iOS

转载 作者:IT王子 更新时间:2023-10-29 08:16:20 27 4
gpt4 key购买 nike

我正在使用:

UIDevice *myDevice = [UIDevice currentDevice];
[myDevice setBatteryMonitoringEnabled:YES];
float batLeft = [myDevice batteryLevel];
int i = [myDevice batteryState];
int batinfo = batLeft * 100;

查找电池状态。我正在寻找如何找到充电完成前的剩余时间。例如:还剩 1 小时 20 分钟。如何以编程方式找到它?

最佳答案

我还没有在 official documentation 中找到任何方法, 既不在类倾销中,private header UIDevice 类。

所以我们必须想出一些办法。我目前想到的最佳“解决方案”类似于估算下载时间时采用的方法:计算下载/充电的平均速度,并将剩余量(数据或费用)除以该速度:

[UIDevice currentDevice].batteryMonitoringEnabled = YES;
float prevBatteryLev = [UIDevice currentDevice].batteryLevel;
NSDate *startDate = [NSDate date];

[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(batteryCharged)
name:UIDeviceBatteryLevelDidChangeNotification
object:nil
];

- (void)batteryCharged
{
float currBatteryLev = [UIDevice currentDevice].batteryLevel;
// calculate speed of chargement
float avgChgSpeed = (prevBatteryLev - currBatteryLev) / [startDate timeIntervalSinceNow];
// get how much the battery needs to be charged yet
float remBatteryLev = 1.0 - currBatteryLev;
// divide the two to obtain the remaining charge time
NSTimeInterval remSeconds = remBatteryLev / avgChgSpeed;
// convert/format `remSeconds' as appropriate
}

关于iphone - 充电完成前的剩余时间,iOS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13899308/

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