如何获取iPhone电池的状态和当前的电量
2014-04-10 10:32
423 查看
- (void)batteryMoniter { UIDevice *device = [UIDevice currentDevice]; device.batteryMonitoringEnabled = YES; if (device.batteryState == UIDeviceBatteryStateUnknown) { NSLog(@"UnKnow"); }else if (device.batteryState == UIDeviceBatteryStateUnplugged){ NSLog(@"Unplugged"); }else if (device.batteryState == UIDeviceBatteryStateCharging){ NSLog(@"Charging"); }else if (device.batteryState == UIDeviceBatteryStateFull){ NSLog(@"Full"); } NSLog(@"%f",device.batteryLevel); } NSLog(@"%f",device.batteryLevel);//简单的这句就是了。 ################################################ Having said that, I would not use a timer event for this, it is pretty inefficient. You want to use KVO instead: - (void) viewWillAppear:(BOOL)animated { UIDevice *device = [UIDevice currentDevice]; device.batteryMonitoringEnabled = YES; currentCharge.text = [NSString stringWithFormat:@"%.2f", device.batteryLevel]; [device addObserver:self forKeyPath:@"batteryLevel" options:0x0 context:nil]; [super viewWillAppear:animated]; } - (void) viewDidDisappear:(BOOL)animated { UIDevice *device = [UIDevice currentDevice]; device.batteryMonitoringEnabled = NO; [device removeObserver:self forKeyPath:@"batteryLevel"]; [super viewDidDisappear:animated]; } - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { UIDevice *device = [UIDevice currentDevice]; if ([object isEqual:device] && [keyPath isEqual:@"batteryLevel"]) { currentCharge.text = [NSString stringWithFormat:@"%.2f", device.batteryLevel]; } }
相关文章推荐
- 如何获取iPhone电池的状态和当前的电量
- 获取当前手机的电池充电状态以及当前的电量
- 如何获取电池电量
- 如何获取当前设备的机型(更新到iPhoneX)?判断机型是否为iPhone X?
- iOS如何获取手机当前的网络状态
- 如何获取当前屏幕状态?左横屏 右横屏 竖屏
- Android端如何获取手机当前的网络状态,比如wifi还是3G, 还是2G, 电信还是联通,还是移动
- android 中如何获取camera当前状态
- Android端如何获取手机当前的网络状态,比如wifi还是3G, 还是2G, 电信还是联通,还是移动
- 获取Android设备电池电量状态
- Android端如何获取手机当前的网络状态,比如wifi还是3G, 还是2G, 电信还是联通,还是移动
- android 获取充电状态 电池电量
- BLE固件开发--如何获取当前连接状态
- 获取Android设备电池电量状态
- 如何获取当前屏幕状态?左横屏 右横屏 竖屏
- Android 获取 Wifi 信号强度,检测是 Wifi 或者流量,获取手机电池电量及充电状态
- 获取手机电池状态,手机内存总容量大小,当前可用容量,手机IP地址,手机当前所连接Wi-Fi的名称
- iOS 如何获取手机型号、系统版本、电池电量
- 获取Android设备电池电量状态