Right so if there's one single thing that is most important out of everything else for a programming language/framework it's the ability to have accurate reliable timers.
So…..who wants a go at solving this puzzle:
setInterval (seems) to work fine when the phone is on. If the display switches of (times out) and the phone locks the timer seems to stop running (according to my mental counting). So i thought "would titanium after all this time still not have one of the most fundamental pieces of functionality - ability to keep track of time regardless of phone state". To test this i added a Ti.Media.vibrate() call inside my setInterval loop and set it for 420sec. Off i switch the phone and hey! It vibrates (more or less on time with every tick). So that's odd i thought - timer works with vibrate, but not without. Until……After leaving it running a few minutes i suddenly noticed the vibrations stopped.
So the natural conclusion is - something times out and the app gets suspended (since for some unknown reason no one though a timer was important enough to fix yet - i guess titanium isn't designed for anything too serious). So i was about to accept this and make the switch to some other framework (or just write it in Java) when all of a sudden - my phone started to vibrate again. But the strange thing is that the vibrations were not uniform - it seemed like the phone was seriously lagging. It would miss a tick and then try to catch up. Sometimes it would vibrate perfectly in time. After a few vibrations it dies again. Sometimes it takes less than 10 seconds before i feel it try to vibrate again. Other times it will just do nothing for a minute or two and start to vibrate again. Usually it only manages 2-3 vibrates before dying.
So the question is… Wtf? Either it works or it doesn't. Either setInterval will fire on time every time regardless of phone state or it will suspend when the phone goes to sleep. So whats up with the odd on/off flaky thing?
Obviously I cant test any of this via the emulator (no vibrate, vm never actually goes into anything I'd call a "sleep" state - the best i could do is make it auto lock). My development device is a Samsung Galaxy S 2.1
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.
Also, since you are trying this under Android, be aware that in Titanium SDK 1.4.x there are many issues with Android which are addressed in the 1.5.0 release.
However, it is still in beta but you could try one of the nightly builds and see if it works better for you. See this page: