IOS中电池变化的监听

Wesley13
• 阅读 644
1,电池级别和电池状态监听通知
[[UIDevice currentDevice] setBatteryMonitoringEnabled:YES];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(checkBattery:) name:UIDeviceBatteryStateDidChangeNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(checkBattery:) name:UIDeviceBatteryLevelDidChangeNotification object:nil];

2,电池的四种状态:
NSArray *stateArray = [NSArray arrayWithObjects: @"Battery state is Unknown", @"Battery is not plugged into a charging source", @"Battery is charging", @"Battery state is full", nil];

 3,[[UIDevice currentDevice] batteryLevel] * 100];//电池的级别,一般都是0.0--1.0之间;

[[UIDevice currentDevice] batteryState];//电池的状态

完整代码:

- (void) doLog: (NSString *) formatstring, ...
{
    va_list arglist;
    if (!formatstring) return;
    va_start(arglist, formatstring);
    NSString *outstring = [[[NSString alloc] initWithFormat:formatstring arguments:arglist] autorelease];
    va_end(arglist);
    [self.log appendString:outstring];
    [self.log appendString:@"\n"];
    self.textView.text = self.log;
}

- (void) checkBattery: (id) sender
{
    NSArray *stateArray = [NSArray arrayWithObjects: @"Battery state is Unknown", @"Battery is not plugged into a charging source", @"Battery is charging", @"Battery state is full", nil];
    self.log = [NSMutableString string];
    [self doLog:@"Battery level: %0.2f%", [[UIDevice currentDevice] batteryLevel] * 100];
    [self doLog:@"Battery state: %@", [stateArray objectAtIndex:[[UIDevice currentDevice] batteryState]]];
}

- (void) viewDidLoad
{
    self.navigationController.navigationBar.tintColor = COOKBOOK_PURPLE_COLOR;

    // Enable battery monitoring
    [[UIDevice currentDevice] setBatteryMonitoringEnabled:YES];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(checkBattery:) name:UIDeviceBatteryStateDidChangeNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(checkBattery:) name:UIDeviceBatteryLevelDidChangeNotification object:nil];

    // Keep checking
    [NSTimer scheduledTimerWithTimeInterval:0.1f target:self selector:@selector(checkBattery:) userInfo:nil repeats:YES];
}
点赞
收藏
评论区
推荐文章
待兔 待兔
2星期前
手写Java HashMap源码
HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22
Wesley13 Wesley13
2年前
Java Web(九)
Listener&FilterListener  监听器1、能做什么事?  监听某一个事件的发生。状态的改变。2、监听器的内部机制  其实就是接口回调.接口回调1、需求:  A在执行循环,当循环到5的时候,通知B。 
公孙晃 公孙晃
12个月前
AlDente Pro for Mac v1.22 最新中文版
是一款适用于Mac操作系统的小工具,可以帮助您限制电池充电量以延长电池寿命。通常情况下,电池在充满的状态下会继续接受电源充电,这可能会导致电池寿命缩短。使用AlDentePro,您可以设置电池只充到特定的充电水平,例如80%或90%,从而减少对电池的压力,
子桓 子桓
12个月前
如何使用AlDente保护电池寿命?
AlDente一款调节macOS充电量阈值的小工具,它可以限制mac最大充电百分比。经常插着电源充电有Macbook用户,会发现电池循环次数很低,但是电池却损耗了很多可用度。因为锂电池并不适合一直保持满电和空电状态,在30%至80%区间的状态下,锂电池的寿
子桓 子桓
9个月前
mac电池最大充电限制 AlDente Pro for Mac最新
AlDentePro是一款适用于Mac操作系统的小工具,可以帮助用户限制电池充电量以延长电池寿命。具体来说,使用AlDentePro,用户可以:设置电池只充到特定的充电水平,例如80%或90%,从而减少对电池的压力,延长其使用寿命。在菜单栏中显示当前电池状
子桓 子桓
8个月前
mac电池最大充电限制推荐: AlDente Pro激活中文最新版
AlDentePro是一款适用于Mac操作系统的电池管理工具,旨在延长电池寿命和提高电池性能。通过限制电池充电的最大百分比,避免电池过度充电,从而减少电池损耗和延长使用寿命。该软件主要的功能包括:限制电池充电量:AlDentePro可以帮助用户限制电池充电
京东云开发者 京东云开发者
4个月前
震惊:苹果手机电池栏“黑白无常”
前言:当程序员👨🏻‍💻遇到难以解决的bug时,大家都会说同样的口头禅:真是见了鬼了(建国后不可以)现象:手机电池栏左黑右白,如下图👈🏻左边的时间是黑色的字体,右边的信号和电池是白色的字体👉🏻,这种感觉就像电池栏在呼喊:我与你之间虽只差一个灵动
赵颜 赵颜
3个月前
MOS产品在储能上的应用介绍与分析
1、储能简介“储能”是通过化学或物理的方法将电能储存起来并在需要时释放的相关技术及措施。依据储存方式,储能可分为机械储能、电磁储能、电化学储能、热储能和化学储能。在不同储能技术中,机械储能中的抽水蓄能是当前商业化应用最为成熟的储能方式,以锂电池、钠硫电池为
赵颜 赵颜
3星期前
简单的电路指示锂离子电池的健康状况
锂离子电池对不良处理很敏感。当我们将电池充电至低于制造商定义的裕量时,可能会发生火灾、爆炸和其他危险情况。锂离子电池在正常使用的过程中,其内部进行电能与化学能相互转化的化学正反应。但在某些条件下,如对其过充电、过放电或过电流工作时,就很容易会导致电池内部发
凿壁偷光 凿壁偷光
1年前
Batteries for Mac(电池电量管理工具)v2.2.8激活版
Batteries是Mac平台上的一款电池电量管理应用。BatteriesMac版可以帮助你在一台Mac电脑上追踪所有设备的电量水平。它适用于任何使用电池的苹果设备,比如你的MacBook、iPhone、iPad、键盘、鼠标。