0

I have developed an iPhone app which gets the microphone recorder inputs every second using NSTimer, calculate the amplitude of sound, and take appropriate action if required. I have noticed that when my iPhone gets locked, my application stops running(it doesn't fire the timer callback function). When I unlock iPhone, it start automatically. Could anyone please tell me how can I allow application responding when iPhone gets locked? Is there any other way around such that iPhone shouldn't get locked when my application is running? What should be Apple's recomendation on this?

I found this article but not sure it is correct way to do.

Thanks.

Black Frog
  • 11,595
  • 1
  • 35
  • 66
Paresh Masani
  • 7,474
  • 12
  • 73
  • 139

1 Answers1

3

You cannot prevent timer from being stopped when application goes into idle state. Only one possible way is to disable idleTimer of the application.

application.idleTimerDisabled = TRUE; 
    //or

//Disable screen dimming if no user input occur on device.
[[UIApplication sharedApplication] setIdleTimerDisabled:YES];
Rahul Vyas
  • 28,260
  • 49
  • 182
  • 256
  • @Rahul: I think I wasn't clear. I don't want to disable timer. I want timer events to get fired but it doesn't happen. I have used following line of code to create the timer `code` levelTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector: @selector(levelTimerCallback) userInfo:nil repeats:YES]; `code` The levelTimerCallback prints the debug message using NSLog. When my iPhone gets locked, I stop receiving this message and when I unlock my iPhone, it again starts printing the message..! – Paresh Masani Apr 27 '11 at 13:53
  • @AppleDeveloper Please read my answer carefully. I have already mentioned the reason "You cannot prevent timer from being stopped when application goes into idle state. Only one possible way is to disable idleTimer of the application." When app goes in idle mode or we can say iPhone goes in sleep mode then all the activities stopped automatically. When phone wakes up all the activities started again. – Rahul Vyas Apr 28 '11 at 06:17
  • Okay. Got it. I think disabling idle timer would not be good solution because it eat up battery. Is there anyway to keep monitoring microphone recording when screen is locked? Basically I do [recorder updatemeters] on timer callback function to get any input recognized by microphone but its not going to work when iPhone gets locked. – Paresh Masani Apr 28 '11 at 13:01
  • Thanks. I know we can record the sound (when screen is locked) by setting appropriate category but as I mentioned I do update meters on timercallback method to get the latest peak and average power values. If timer doesn't work when screen is locked, how/where should I do update meters? I researched enough but couldn't find anything useful. – Paresh Masani Apr 28 '11 at 14:48
  • see this http://stackoverflow.com/questions/5187741/nstimer-on-background-is-working also see this http://www.iphonedevsdk.com/forum/iphone-sdk-development/58643-keep-nstimer-running-when-app-background-multitasking.html – Rahul Vyas Apr 29 '11 at 02:36