简体   繁体   English

监控iPhone上的电池电量

[英]Monitoring battery level on iPhone

I'm using the basic battery monitoring code on iOS and I've read somewhere that it drains battery to do so. 我在iOS上使用基本的电池监控代码,并且在某处读到它会消耗电池电量。

UIDevice *device = [UIDevice currentDevice];
    device.batteryMonitoringEnabled = YES;
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(batteryChanged) name:@"UIDeviceBatteryLevelDidChangeNotification" object:device];

I can't really find any good reference with actual data on this. 我真的找不到有关此方面实际数据的任何参考。

Is it really bad to check the changes in battery level? 检查电池电量的变化真的不好吗? What other possibilities do I have? 我还有其他可能性吗? I would like to do much less treatment when the battery is low so that I don't finish it off. 当电池电量不足时,我想少做些治疗,以免结束工作。

Use this: 用这个:

 UIDevice *myDevice = [UIDevice currentDevice];
 [myDevice setBatteryMonitoringEnabled:YES];
 double batLeft = (float)[myDevice batteryLevel] * 100;

I can think that the continous battery level monitoring is expensive. 我可以认为连续的电池电量监测非常昂贵。 You may want to not monitor the battery continously . 您可能不希望连续监视电池

Set a timer to every 5 minutes. 将计时器设置为每5分钟一次。 In the tick method, set monitoring to YES, get level, then set monitoring to NO again. 在滴答方法中,将监视设置为“是”,获取级别,然后再次将监视设置为“否”。 Don't need to subscribe to any notifications this way. 不需要以这种方式订阅任何通知。

You may increase the interval below 20% battery, or so. 您可以将间隔增加到20%以下的电池以下。

-(void)tick:(NSTimer*) timer
{
    [[UIDevice currentDevice] setBatteryMonitoringEnabled:YES];
    self.batteryPercentace = [[UIDevice currentDevice] batteryLevel];
    [[UIDevice currentDevice] setBatteryMonitoringEnabled:NO];
}

I'm not sure if it needs to be monitored at all, you may try out just simply read battery level. 我不确定是否需要对其进行监视,您可以尝试仅读取电池电量就可以尝试一下。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM