IOS中电池变化的监听

11,电池级别和电池状态监听通知 2[[UIDevice currentDevice] setBatteryMonitoringEnabled:YES]; 3 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(checkBattery:) name:UIDeviceBatteryStateDidChangeNotification object:nil]; 4 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(checkBattery:) name:UIDeviceBatteryLevelDidChangeNotification object:nil]; 5 62,电池的四种状态: 7NSArray *stateArray = [NSArray arrayWithObjects: @"Battery state is Unknown", @"Battery is not plugged into a charging source", @"Battery is charging", @"Battery state is full", nil]; 8 9 3[[UIDevice currentDevice] batteryLevel] * 100];//电池的级别,一般都是0.0--1.0之间; 10 11[[UIDevice currentDevice] batteryState]//电池的状态 12 13完整代码: 14 15- (void) doLog: (NSString *) formatstring, ... 16{ 17 va_list arglist; 18 if (!formatstring) return; 19 va_start(arglist, formatstring); 20 NSString *outstring = [[[NSString alloc] initWithFormat:formatstring arguments:arglist] autorelease]; 21 va_end(arglist); 22 [self.log appendString:outstring]; 23 [self.log appendString:@"\n"]; 24 self.textView.text = self.log; 25} 26 27- (void) checkBattery: (id) sender 28{ 29 NSArray *stateArray = [NSArray arrayWithObjects: @"Battery state is Unknown", @"Battery is not plugged into a charging source", @"Battery is charging", @"Battery state is full", nil]; 30 self.log = [NSMutableString string]; 31 [self doLog:@"Battery level: %0.2f%", [[UIDevice currentDevice] batteryLevel] * 100]; 32 [self doLog:@"Battery state: %@", [stateArray objectAtIndex:[[UIDevice currentDevice] batteryState]]]; 33} 34 35- (void) viewDidLoad 36{ 37 self.navigationController.navigationBar.tintColor = COOKBOOK_PURPLE_COLOR; 38 39 // Enable battery monitoring 40 [[UIDevice currentDevice] setBatteryMonitoringEnabled:YES]; 41 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(checkBattery:) name:UIDeviceBatteryStateDidChangeNotification object:nil]; 42 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(checkBattery:) name:UIDeviceBatteryLevelDidChangeNotification object:nil]; 43 44 // Keep checking 45 [NSTimer scheduledTimerWithTimeInterval:0.1f target:self selector:@selector(checkBattery:) userInfo:nil repeats:YES]; 46}
点赞
收藏

评论区

加载中...

相关推荐

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

Java Web(九)

Listener&FilterListener  监听器1、能做什么事?  监听某一个事件的发生。状态的改变。2、监听器的内部机制  其实就是接口回调.接口回调1、需求:  A在执行循环,当循环到5的时候,通知B。 

如何使用AlDente保护电池寿命?

AlDente一款调节macOS充电量阈值的小工具,它可以限制mac最大充电百分比。经常插着电源充电有Macbook用户,会发现电池循环次数很低,但是电池却损耗了很多可用度。因为锂电池并不适合一直保持满电和空电状态,在30%至80%区间的状态下,锂电池的寿

mac电池最大充电限制 AlDente Pro for Mac最新

AlDentePro是一款适用于Mac操作系统的小工具,可以帮助用户限制电池充电量以延长电池寿命。具体来说,使用AlDentePro,用户可以:设置电池只充到特定的充电水平,例如80%或90%,从而减少对电池的压力,延长其使用寿命。在菜单栏中显示当前电池状

mac电池最大充电限制推荐: AlDente Pro激活中文最新版

AlDentePro是一款适用于Mac操作系统的电池管理工具,旨在延长电池寿命和提高电池性能。通过限制电池充电的最大百分比,避免电池过度充电,从而减少电池损耗和延长使用寿命。该软件主要的功能包括:限制电池充电量:AlDentePro可以帮助用户限制电池充电

震惊:苹果手机电池栏“黑白无常”

前言:当程序员👨🏻‍💻遇到难以解决的bug时,大家都会说同样的口头禅:真是见了鬼了(建国后不可以)现象:手机电池栏左黑右白,如下图👈🏻左边的时间是黑色的字体,右边的信号和电池是白色的字体👉🏻,这种感觉就像电池栏在呼喊:我与你之间虽只差一个灵动