现在的方法是5s钟check一次电池状态,轮询
有什么办法可以在状态发生改变的时候主动通知吗

解决方案 »

  1.   

    [[UIDevice currentDevice] setBatteryMonitoringEnabled:YES];UIDeviceBatteryStateDidChangeNotification
    UIDeviceBatteryLevelDidChangeNotification监听这两个消息应该就可以了.
      

  2.   

       通过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]]);}