I'm a little confused by the currently accepted answer which seems to imply that using Handler
on a non-UI thread isn't possible, because it's something I've done quite routinely and I thought it was pretty well known.
Within a non-UI Thread
:
@Override
public void run() {
Looper.prepare();
...
mThreadHandler = new Handler() {
public void handleMessage(Message msg) {
switch (msg.what) {
...
break;
default:
break;
}
}
};
Looper.loop();
}
Using the mThreadHandler
, messages can be sent to be processed by the Handler
in the above non-UI Thread
.
If there are any good reasons for not using the Handler
/ Message
classes to post work to be done on a non-UI Thread in this way then I'd like to know. It has been working fine for me so far though. The only reason I've read for not using a Handler
in this way is that "a Handler
is meant for posting stuff to the UI thread" which is not in itself a good technical basis.