IOS中电池变化的监听

Wesley13
• 阅读 586
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];
}
点赞
收藏
评论区
推荐文章
Wesley13 Wesley13
2年前
adb shell dumpsys [options]操作实例讲解
adbshelldumpsys\options\该命令用于打印出当前系统信息,默认打印出设备中所有service的信息。由于service比较多,这里选几个用的比较多的service来进行讲解:battery电池状态batteryp
Wesley13 Wesley13
2年前
Java Web(九)
Listener&FilterListener  监听器1、能做什么事?  监听某一个事件的发生。状态的改变。2、监听器的内部机制  其实就是接口回调.接口回调1、需求:  A在执行循环,当循环到5的时候,通知B。 
绣鸾 绣鸾
9个月前
Mac系统硬件运行信息查看器 iStat Menus
iStatMenus是一款Mac电脑上的系统监控工具,它可以帮助用户监测电脑的硬件和软件状况,提供实时的系统数据和统计信息。其主要特点包括:1.系统监测:iStatMenus可以监测CPU、内存、硬盘、网络、电池等系统参数,方便用户了解电脑的运行状态。2.
公孙晃 公孙晃
8个月前
AlDente Pro for Mac v1.22 最新中文版
是一款适用于Mac操作系统的小工具,可以帮助您限制电池充电量以延长电池寿命。通常情况下,电池在充满的状态下会继续接受电源充电,这可能会导致电池寿命缩短。使用AlDentePro,您可以设置电池只充到特定的充电水平,例如80%或90%,从而减少对电池的压力,
子桓 子桓
8个月前
如何使用AlDente保护电池寿命?
AlDente一款调节macOS充电量阈值的小工具,它可以限制mac最大充电百分比。经常插着电源充电有Macbook用户,会发现电池循环次数很低,但是电池却损耗了很多可用度。因为锂电池并不适合一直保持满电和空电状态,在30%至80%区间的状态下,锂电池的寿
绣鸾 绣鸾
6个月前
Mac系统硬件活动查看器iStat Menus
iStatMenus是一款Mac电脑上的系统监控工具,它可以帮助用户监测电脑的硬件和软件状况,提供实时的系统数据和统计信息。其主要特点包括:1.系统监测:iStatMenus可以监测CPU、内存、硬盘、网络、电池等系统参数,方便用户了解电脑的运行状态。2.
子桓 子桓
5个月前
mac电池最大充电限制 AlDente Pro for Mac最新
AlDentePro是一款适用于Mac操作系统的小工具,可以帮助用户限制电池充电量以延长电池寿命。具体来说,使用AlDentePro,用户可以:设置电池只充到特定的充电水平,例如80%或90%,从而减少对电池的压力,延长其使用寿命。在菜单栏中显示当前电池状
子桓 子桓
5个月前
mac电池最大充电限制推荐: AlDente Pro激活中文最新版
AlDentePro是一款适用于Mac操作系统的电池管理工具,旨在延长电池寿命和提高电池性能。通过限制电池充电的最大百分比,避免电池过度充电,从而减少电池损耗和延长使用寿命。该软件主要的功能包括:限制电池充电量:AlDentePro可以帮助用户限制电池充电
京东云开发者 京东云开发者
1个月前
震惊:苹果手机电池栏“黑白无常”
前言:当程序员👨🏻‍💻遇到难以解决的bug时,大家都会说同样的口头禅:真是见了鬼了(建国后不可以)现象:手机电池栏左黑右白,如下图👈🏻左边的时间是黑色的字体,右边的信号和电池是白色的字体👉🏻,这种感觉就像电池栏在呼喊:我与你之间虽只差一个灵动
凿壁偷光 凿壁偷光
1年前
Batteries for Mac(电池电量管理工具)v2.2.8激活版
Batteries是Mac平台上的一款电池电量管理应用。BatteriesMac版可以帮助你在一台Mac电脑上追踪所有设备的电量水平。它适用于任何使用电池的苹果设备,比如你的MacBook、iPhone、iPad、键盘、鼠标。