您的位置:首页 > 其它

获取当前手机的电池充电状态以及当前的电量

2015-08-11 15:59 363 查看
获取当前手机的电池充电状态以及当前的电量

- (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];

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: