0

So I have an large (up to 5000) array of pre-defined, time-senstive messages I want my program to send to a Handler. Some of these messages need to be as little as 40 milliseconds apart. I thought I could just call a bunch of sendMessageAtTime() to queue them up and my Handler would be called for each one at the appropriate time.

Unfortunately, at least in my AVD, it appears to execute the handler a few times then not do anything until the last message is sent. Is the Handler not keeping up? (i.e. can't finish before the next time it's called?)

Does sendMessageAtTime() wait to send the message until the Handler is done with its current message?

What happens if the specified time to send the message is in the past? Does it drop the message entirely or does it send it before sending any other "later" messages in the queue?

Are there any techniques to make the Handler "keep up"? For example, I could include the requested time in the message to the Handler so that the Handler could do nothing if it determines it's too late.

Thanks.

ScottyB
  • 2,167
  • 1
  • 30
  • 46
  • "So I have an large (up to 5000) array of pre-defined, time-senstive messages I want my program to send to a Handler" -- for the love of `$DEITY`, why? "Some of these messages need to be as little as 40 milliseconds apart" -- Android is not a real-time operating system. "Does sendMessageAtTime() wait to send the message until the Handler is done with its current message?" -- yes, because it is single-threaded. "Are there any techniques to make the Handler "keep up"?" -- among other things, don't send 5000 timed messages to it. – CommonsWare Feb 05 '14 at 18:57
  • I agree that's not a very scalable approach! So what I ended up doing is timing the sending of the messages and sending them via sendMessageAtFrontOfQueue() so that the handler will "keep up." I'm also sending a fixed "end time" to the handler by which all messages should be processed. The handler checks the "real time" against the end time and ignores all messages that arrive too late. Is there a better way? – ScottyB Feb 05 '14 at 21:50
  • Since I have no idea what these 5000 messages are for, I cannot say whether there is a better way. It's certainly a significant code smell, IMHO. – CommonsWare Feb 05 '14 at 22:02
  • Since you asked, it's an array of colors and the time at which they should be displayed. For example: #111111 at 0 ms, #222222 at 43 ms, #333333 at 102 ms, etc. Colors need to display at the time given, so I don't want to wait for a previous color. I am using sleep(myStartTime + duration - SystemClock.uptimeMillis() to determine when to send the message to change colors. – ScottyB Feb 05 '14 at 22:20
  • Why aren't you using an `AnimationDrawable`, then? "I am using sleep(myStartTime + duration - SystemClock.uptimeMillis() to determine when to send the message to change colors" -- that's going to be so inaccurate, it's not even funny. You cannot precisely time things that way using the main application thread. I'm not even sure an `AnimationDrawable` would be more reliable, but it'd certainly be less ugly. Beyond that, you're looking at using game development techniques, and certainly not a `Handler`. – CommonsWare Feb 05 '14 at 22:23
  • Thanks for the tip. It's all so new... – ScottyB Feb 05 '14 at 22:40
  • So I'm pretty sure I can programatically create the AnimationDrawable I need, but is there a way to store the object as a resource for later access? – ScottyB Feb 06 '14 at 15:46
  • You can define an `AnimationDrawable` as a resource, which is what I had in mind with the comment. Sorry for not making that part clear. See [the `AnimationDrawable` JavaDocs](https://developer.android.com/reference/android/graphics/drawable/AnimationDrawable.html) and https://developer.android.com/guide/topics/graphics/drawable-animation.html. It's more verbose than I remember -- I forgot that the individual frames were `` elements rather than arbitrary XML-defined drawable constructs. My apologies again. – CommonsWare Feb 06 '14 at 15:51
  • My problem is that I don't know what the AnimationDrawable will contain until run time. I made an attempt to write an AnimationDrawable xml file and multiple Drawable xml files (frames) and store them. The documentation says they go in the /res/drawable folder but actually there are multiple "drawable" folders (hdpi, mdpi, etc.). I have a feeling writing directly to these locations is frowned upon. (Maybe just one of them and hope for the best?) That's why I asked if I create the AnimationDrawable with Java, can I then store it as a resource for later access. Thanks again. – ScottyB Feb 06 '14 at 16:11
  • "I have a feeling writing directly to these locations is frowned upon" -- it's impossible, as resources are read-only at runtime. There's no good way to persist an `AnimationDrawable` directly. If you elect to stick with the `AnimationDrawable` route, I'd focus instead on writing code to build one up from some other persistable representation (XML, JSON, etc.). – CommonsWare Feb 06 '14 at 17:08
  • That's exactly what I will do tonight. Thanks again. – ScottyB Feb 06 '14 at 17:52

0 Answers0