Continue Timer even if device falls asleep

I’m making a meditation timer with two short audio files.  One audio file (4 minutes) starts the meditation session.  The 2nd audio file (4.5 minutes)  ends the meditation session.

I’m using a  timer.performWithDelay for the silent meditation time which triggers a function to the play the 2nd audio file.

Using the Simulator, it works great.  But using the actual iPad, how to continue the timer when the iPad falls asleep?

Or is there a way to program the user’s iOS device to “Auto Lock = Never” while timer is running.

Then stealthily set it back to the user’s setting when the app is closed?

Pipe dream?   Only in the movies?

See this on how to prevent device from sleeping: http://docs.coronalabs.com/daily/api/library/system/setIdleTimer.html

Perfect!  Thanks, Jon.

Just for further info…

You can continue playing audio whilst the device sleeps (on iOS at least) but cannot continue executing code whilst the device sleeps (with some exceptions like GPS for instance).

So aside from the keeping the device active route, another potential solution would be to save the timers current time to a file when the device sleeps (presuming you get an application suspend event when that happens?), then when the app resumes you read the last time from a file then restart the timer from the previously saved time.

I’m not sure if this will suit your workflow, but just wanted to give you some additional information for further reference.

Hope this helps.

The other thing to consider is local notifications if you need reminders set for the future.

Rob

Thanks for both of those alternate routes, Gremlin and Rob. 

The local notification seems it  wouldn’t drain the battery like the timer.performWithDelay  but it seems from the link below, there are no options to pause and resume which I need to be part of the app.

http://docs.coronalabs.com/guide/events/appNotification/index.html

Another idea is to use a countdown timer along with timer.performWithDelay and have it trigger the function for the 2nd audio file.  This may be a better way to do this since I can also have a visual countdown if someone wants to check how much time is left in the session.

http://coronalabs.com/blog/2013/01/15/working-with-time-and-dates-in-corona/

You could code the suspend/resume events to set and cancel the notification and re-enable the timer to run while the app is active.  It will take a little work, but I’m pretty sure that’s how other timer type apps work.

Rob

Or is there a way to program the user’s iOS device to “Auto Lock = Never” while timer is running.

Then stealthily set it back to the user’s setting when the app is closed?

Pipe dream?   Only in the movies?

See this on how to prevent device from sleeping: http://docs.coronalabs.com/daily/api/library/system/setIdleTimer.html

Perfect!  Thanks, Jon.

Just for further info…

You can continue playing audio whilst the device sleeps (on iOS at least) but cannot continue executing code whilst the device sleeps (with some exceptions like GPS for instance).

So aside from the keeping the device active route, another potential solution would be to save the timers current time to a file when the device sleeps (presuming you get an application suspend event when that happens?), then when the app resumes you read the last time from a file then restart the timer from the previously saved time.

I’m not sure if this will suit your workflow, but just wanted to give you some additional information for further reference.

Hope this helps.

The other thing to consider is local notifications if you need reminders set for the future.

Rob

Thanks for both of those alternate routes, Gremlin and Rob. 

The local notification seems it  wouldn’t drain the battery like the timer.performWithDelay  but it seems from the link below, there are no options to pause and resume which I need to be part of the app.

http://docs.coronalabs.com/guide/events/appNotification/index.html

Another idea is to use a countdown timer along with timer.performWithDelay and have it trigger the function for the 2nd audio file.  This may be a better way to do this since I can also have a visual countdown if someone wants to check how much time is left in the session.

http://coronalabs.com/blog/2013/01/15/working-with-time-and-dates-in-corona/

You could code the suspend/resume events to set and cancel the notification and re-enable the timer to run while the app is active.  It will take a little work, but I’m pretty sure that’s how other timer type apps work.

Rob