获取当前手机的电池充电状态以及当前的电量
- (void)viewDidLoad {
[super viewDidLoad];
//打开电池的监听
[UIDevice currentDevice].batteryMonitoringEnabled = YES;
//获取电池的状态
UIDeviceBatteryState BatteryState = [UIDevice currentDevice].batteryState;
//获取剩余电量 范围在0.000000 至 1.000000之间
CGFloat batterylevel = [UIDevice currentDevice].batteryLevel;
//battery的状态
[UIDevice currentDevice].batteryState分为:
UIDeviceBatteryStateUnknown, 未知
UIDeviceBatteryStateUnplugged, // 未充电
UIDeviceBatteryStateCharging, // 正在充电
UIDeviceBatteryStateFull, // 满电
if (BatteryState == UIDeviceBatteryStateUnknown) {
NSLog(@"unknow");
}else{
NSLog(@"know");
//将剩余的电量用label显示。
self.current.text = [NSString stringWithFormat:@"%f",batterylevel];
}
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文地址:http://blog.csdn.net/lu_ca/article/details/47421611