如何实时监听电池状态是否发生改变 现在的方法是5s钟check一次电池状态,轮询有什么办法可以在状态发生改变的时候主动通知吗 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 [[UIDevice currentDevice] setBatteryMonitoringEnabled:YES];UIDeviceBatteryStateDidChangeNotificationUIDeviceBatteryLevelDidChangeNotification监听这两个消息应该就可以了. 通过UIDevice获取电池用量和状态转换。 UIDevice *device = [UIDevice currentDevice]; device.batteryMonitoringEnabled = YES; //开启了监视电池状态的功能 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(batteryLevelChanged:) name:@"UIDeviceBatteryLevelDidChangeNotification" object:device]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(batteryStateChanged:) name:@"UIDeviceBatteryStateDidChangeNotification" object:device];- (void)batteryLevelChanged:(id)sender{ UIDevice *myDevice = [UIDevice currentDevice]; [myDevice setBatteryMonitoringEnabled:YES]; float batteryLevel = [myDevice batteryLevel]; NSLog(@"电池剩余比例:%@", [NSString stringWithFormat:@"%f",batteryLevel*100]);}-(void)batteryStateChanged:(id)sender{ NSArray *stateArray = [NSArray arrayWithObjects:@"未开启监视电池状态",@"电池未充电状态",@"电池充电状态",@"电池充电完成",nil]; NSLog(@"电池状态:%@", [stateArray objectAtIndex:[[UIDevice currentDevice] batteryState]]);} 数据库读取NSData遇到问题了 uiwebview上的一些问题 有没有宏可以区别debug模式和release模式? 苹果开发内存控制-(转自苹果中国官网) 为什么我的程序退出前不进viewWillDisappear?? 我想做一个简单的uiwebview加载一个网页怎么弄? IOS(越狱)下 U3D 游戏 C#-DLL 的代码热更新 是否可行? 请问同名同类型的certificate ,怎么理解? IOS统一标题栏的右侧按钮 iOS开发,刚接手React-native,遇到的问题,求教! 静态库导入问题 苹果开发者注册提示双重认证
UIDeviceBatteryLevelDidChangeNotification监听这两个消息应该就可以了.
UIDevice *device = [UIDevice currentDevice];
device.batteryMonitoringEnabled = YES; //开启了监视电池状态的功能
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(batteryLevelChanged:) name:@"UIDeviceBatteryLevelDidChangeNotification" object:device]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(batteryStateChanged:) name:@"UIDeviceBatteryStateDidChangeNotification" object:device];
- (void)batteryLevelChanged:(id)sender
{ UIDevice *myDevice = [UIDevice currentDevice]; [myDevice setBatteryMonitoringEnabled:YES]; float batteryLevel = [myDevice batteryLevel]; NSLog(@"电池剩余比例:%@", [NSString stringWithFormat:@"%f",batteryLevel*100]);}-(void)batteryStateChanged:(id)sender
{ NSArray *stateArray = [NSArray arrayWithObjects:@"未开启监视电池状态",@"电池未充电状态",@"电池充电状态",@"电池充电完成",nil]; NSLog(@"电池状态:%@", [stateArray objectAtIndex:[[UIDevice currentDevice] batteryState]]);}