views:

197

answers:

2

My Problem is that I need to send messages with a delay of 1 second. The handler then initiates some action, you're getting the picture.

There are nevertheless some conditions in which the already sent message should be deleted ( before the second elapsed ) to prevent the handler from doing anything. I couldn't figure out how to do this ( or if it's even possible ), so If anyone of you has a clue, please let me know..

+1  A: 

Well, there are removeMessages() methods on Handler, but they seem somewhat scary. I don't think there are other alternatives for deleting selected messages out of the Handler's queue. Instead, I'd focus on ensuring your Handler knows the messages to ignore.

CommonsWare
Hm I'm afraid that's indeed the way to go. Seems like a design flaw, though.
moritz
+2  A: 

There is nothing scary about the removeMessages() methods; they are perfectly safe. The framework relies heavily on these methods and they are used in many many places, especially in the default widgets (View, ListView, etc.) It's much much better than building a Handler that ignores specific messages. This is programming, don't go with your feelings :p

Romain Guy
It would help if there were more documentation on those methods, if the 'what' parameter were available on all enqueuing mechanisms (e.g., postRunnable()), if the distinction between 'what' and 'token' were clearer, etc. If I have to rummage through Android source code to figure out what Handler is doing today, that means I have no guarantees that such undocumented behavior will remain consistent tomorrow. Hence, removeMessages() are, IMHO, scary for developers to rely upon. It won't take much to clear this up, but I can't make decisions for what is and is not appropriate Handler behavior.
CommonsWare
that last sentence of yours made my day, clearly. But besides, I also agree with commonsaware that the documentation is really insufficient in that field, maybe I should just file a ticket.
moritz
There's no reason to have a "what" when you post a Runnable, you just use removeCallback() for this.What exactly is not guaranteed from the documentation?
Romain Guy