重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
#import"ViewController.h"
创新互联公司主要从事成都做网站、网站设计、网页设计、企业做网站、公司建网站等业务。立足成都服务盘龙,十余年网站建设经验,价格优惠、服务专业,欢迎来电咨询建站服务:028-86922220
#import
@interfaceViewController ()
//@property(nonatomic,readonly) float batteryLevel NS_AVAILABLE_IOS(3_0);
// 0 .. 1.0. -1.0 if UIDeviceBatteryStateUnknown它返回的是0.00-1.00之间的浮点值。
@end
@implementation ViewController
- (void)viewDidLoad {
[superviewDidLoad];
//打开电池的监听
[UIDevicecurrentDevice].batteryMonitoringEnabled =YES;
//获取电池的状态
UIDeviceBatteryState BatteryState = [UIDevicecurrentDevice].batteryState;
//获取剩余电量范围在0.000000至 1.000000之间
CGFloat batterylevel = [UIDevicecurrentDevice].batteryLevel;
//battery的状态分为:
/*
UIDeviceBatteryStateUnknown, 未知
UIDeviceBatteryStateUnplugged, //未充电
UIDeviceBatteryStateCharging, //正在充电
UIDeviceBatteryStateFull, //满电
*/
if (BatteryState ==UIDeviceBatteryStateUnknown) {
self.dlLabel.text = [NSStringstringWithFormat:@"%0.0f%%",batterylevel*100];
NSLog(@"unknow");
}else{
NSLog(@"know");
//将剩余的电量用label显示。
self.dlLabel.text = [NSStringstringWithFormat:@"%0.0f%%",batterylevel*100];
}
//
// [self getCurrentBatteryLevel];
// [UIDevice currentDevice].batteryMonitoringEnabled = YES;
// [[NSNotificationCenter defaultCenter]
// addObserverForName:UIDeviceBatteryLevelDidChangeNotification
// object:nil queue:[NSOperationQueue mainQueue]
// usingBlock:^(NSNotification *notification) {
// // Level has changed
// dl=[UIDevice currentDevice].batteryLevel;
// NSLog(@"电池电量:%.2f", [UIDevice currentDevice].batteryLevel);
//
// self.dlLabel.text = [NSString stringWithFormat:@"%f",[UIDevice currentDevice].batteryLevel];
//
// }];
// Do any additional setup after loading the view, typically from a nib.
}