4

Is it possible to check battery level while our application is in background?

Currently I am working on one iOS application in which user will be alerted when battery reached to certain level.

I have searched on google/stake overflow . but find none of the useful. I can determine the battery level while application is in foreground state. But we all know that apple does not allow application to be run in background.

So My question is how can I get the current battery level event even if my application is in background.

Here is the similar application. Its a paid application. But by seeing screenshot, you will get the idea want I trying to say.

https://itunes.apple.com/it/app/battery-alert!/id416283462?mt=8

rmaddy
  • 314,917
  • 42
  • 532
  • 579
iRoid Solutions
  • 415
  • 6
  • 20
  • possible duplicate of [Can I check battery status of iPhone in background state?](http://stackoverflow.com/questions/7705373/can-i-check-battery-status-of-iphone-in-background-state) – Pang Dec 01 '13 at 09:24

1 Answers1

0
UIDevice *device = [UIDevice currentDevice];
device.batteryMonitoringEnabled = YES;
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(batteryChanged:) name:@"UIDeviceBatteryLevelDidChangeNotification" object:device];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(batteryChanged:) name:@"UIDeviceBatteryStateDidChangeNotification" object:device];

// Your notification handler
- (void)batteryChanged:(NSNotification *)notification
{
    UIDevice *device = [UIDevice currentDevice];
    NSLog(@"state: %i | charge: %f", device.batteryState, device.batteryLevel);
}

check out this code...

Jitendra
  • 5,055
  • 2
  • 22
  • 42
  • I will try this code. BUt I doubt whether it will work in background or not. – iRoid Solutions Oct 01 '13 at 05:21
  • 1
    Does not work in the background. [A suspended app does not execute any code and therefore cannot process notifications, including battery change notifications](https://developer.apple.com/library/ios/DOCUMENTATION/iPhone/Conceptual/iPhoneOSProgrammingGuide/ManagingYourApplicationsFlow/ManagingYourApplicationsFlow.html#//apple_ref/doc/uid/TP40007072-CH4-SW45). – Pang Dec 01 '13 at 09:30