I have a thread (guts implemented as a subclass of Runnable) whose job is to perform an operation on demand from other threads or automatically every two minutes if no other thread invokes the operation. The run() method of this Runnable calls Looper.prepare(), creates a Handler object in a member variable, uses that to call sendEmptyMessageDelayed(1, 3000), then calls Looper.loop().
Other threads use a method in this thread's public API to invoke its operation. That method just calls the handler's sendEmptyMessage(0) method to invoke it. When the handler's handleMessage(Message) method is called, it acquires a lock (ReentrantLock), calls the handler's removeCallbacksAndMessages(null) method and performs its operation (within a try/catch block). The finally section of that block calls the handler's sendEmptyMessageDelayed(1, 120000) method then releases the lock. This works fine on every device I have tested it on, and by everything I know, it is a valid way to do this that ought to work on every version of Android OS. Recently, however, somebody claimed it wasn't running every two minutes as it should, and gave me the log file it produces to prove it (which it does). That log shows that sendEmptyMessageDelayed(1, 120000) was called, and then 11 minutes later, the handleMessage(Message) method was called. I know this invocation of handleMessage() was in response to that sendEmptyMessageDelayed(1, 120000) call because Message.what was 1 (a value used only by the sendEmptyMessageDelayed() call). How is this possible? I could understand the timing being off by a few seconds, but 11 minutes? In subsequent attempts to reproduce this, I have seen that timing being off by 45 minutes and more. This problem has been observed on an HTC One X running Android 4.0.3 and a Samsung Galaxy SII running OS 2.3.6. Has anybody else seen this issue? Does anybody have a suggestion for what might be causing this? My current position is that this is an OS bug, but I am loathe to just accept that and call it a day. BTW - just for kicks, I temporarily re-implemented this using a Timer object instead, and observed exactly the same problem (using Handler is a better solution anyway). Thanks, Mark Peters -- 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