Brenton,

I'm having a sense of deja vu, but here again are some points about timers in Android.

A Java Timer / TimerTask is not really good for an Android application:

- TimerTasks are invoked on a background thread, this creates an inconvenience if you need to touch the UI, since UI components in Android can only be touched from the original thread that created them ("the UI thread").

- TimerTasks are implemented entirely in the application, Android has no knowledge of them. This means that if Android terminates your application or its service for whatever reason, then the timer task goes away as well and will not be executed.

[ this probably what's happening in your tests ]

- If the phone goes to sleep, your timer task will not be executed.

The proper mechanism is to use AlarmManager and schedule an alarm:

- Alarms can fire a broadcast action, which you would then handle in a broadcast receiver. This action is fired and delivered on the UI thread, so thread switching tricks are not necessary;

- Android has knowledge of alarms, and if your application has been terminated by the time the alarm fires, Android will start it again;

- You can request that the alarm wake up the phone from sleep (you don't want your eggs boiled the wrong way or the pot destroyed by heat, just because the phone goes to sleep).

SDK docs:

http://developer.android.com/reference/android/app/AlarmManager.html

Someone's blog, looks like a nice write-up:

http://justcallmebrian.com/?p=129

One more thing: if you schedule an alarm that wakes up the phone, you will need to use a WakeLock to keep the phone running (presumably so it can display the alert about eggs being ready).

http://www.androidguys.com/2009/04/02/wake-up-with-the-alarm/

Hope this helps.

-- Kostya

30.01.2011 16:41, bklik пишет:
I'm trying to write an egg timer application.  Here's what I have:

* An Activity that has the UI for adding timer and displaying time
remaining
* A Service that has a Timer/TimerTask which controls the countdown

When I set a really long time, say nine hours and go to bed, somewhere
in the night something cancels the timer. According the the
Applications menu the app is still "running" and the Running Services
shows that the Service is still running, but the Timer is no longer
sending "ticks." I've put Logs all over every function to see if
something's getting called, but nothing does!

What cancels Timer/TimerTasks? How do you keep them running in
services?

I've tried using a partial WakeLock, but it failed.

Brenton



--
Kostya Vasilyev -- WiFi Manager + pretty widget -- http://kmansoft.wordpress.com

--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to