I want to have a worker thread that can 1) send messages to the UI thread to change things in the GUI, and 2) receive messages from the UI thread to control the worker thread's behavior.
The first part is easy: Create a Handler in the UI thread and let the worker thread send messages to it. Works like a charm. The worker thread uses Thread.sleep() to perform an efficient delay, wakes up to "do things", sends an update to the UI thread's Handler, and repeats.
The next part is apparently difficult in Android. The worker thread must have a Looper to create a Handler so it can RECEIVE messages, and Looper.loop() is a blocking call which means the worker thread is now 100% committed to running the Looper and can't do the other things it is currently doing ("do things", send update message, sleep, repeat).
This implies I need a THIRD thread just to run the Looper for the worker thread, which is ridiculous. It should be possible to have the worker thread do useful things, AND process a message if and when one arrives. This yields an efficient system with the minimum number of thread objects and minimal overhead. I can't figure out how to implement this behavior in Android.
So... how does a worker thread support a Handler and do anything else useful? Thanks!