I've been trying to get my head around this and struggling for days! I have a countdown timer running in my app that shows an alert when the time is up.
The problem is when the app goes to sleep or the device is shut down.
My timer code is situated in app.js which fires an event to update the label text in another file. I have managed to get this working on IoS using background services that re kickstarts the timer - but having no joy with Android.
Ideally I'd like to get the timer running again so when the user re-opens the app the timer text is correct, but struggling with this.
I've tried to play around with services and intents, but I am really struggling to understand how this works. Is it possible to create something that will ensure the timer text is correct when the user re-opens the app; or failign this create an alert at the very least when the timer is finished?
Any help, as always, is massively appreciated. I've read the wiki guide on simple services and explored the kitchen sink, but I'm not even sure if that's the way I need to go as I don't really understand the concept that well.
Check out the wiki for Scheduling Future Notifications. This has a complete example for Android.
Android Timer (even when the phone goes to sleep)
When the phone goes to sleep, the app is “paused” and therefore, the timer is paused. To solve this issue on Android, I added the WAKE_LOCK permission to tiapp.xml:
(…) <android xmlns:android="http://schemas.android.com/apk/res/android"> <manifest android:versionName="1.0.0"> <uses-permission android:name="android.permission.WAKE_LOCK" /> </manifest> <services> <service type="interval" url="myservice.js"/> </services> </android> (…)
I am using an interval service that updates a progress bar; for an example, please look at ‘examples/android_services.js’ in the Kitchen Sink.
I use the ildeTimerDisabled
Ti.App.idleTimerDisabled = true; // to disable going to sleep (iPhone only) Ti.App.idleTimerDisabled = false; // to re-enable it
Hope this helps.