Yaroslav Mytkalyk Yaroslav Mytkalyk - 5 months ago 21
Android Question

Run Handler messages in a background thread

I want to run some Runnable in a background thread. I want to use Handler because it's convenient for delays.
What I mean is

handler.post(runnable, delay);


Where runnable should be run in background Thread.
Is it possible to create such Handler?
Is there a "background" Looper somewhere or how can I create it?

P.S. I know how to do it with a custom class extends Thread but it requires a little more coding effort than doing it the handler way. So please don't post other solutions or something like

handler.post(new Runnable() {
@Override
public void run() {
new Thread() {
@Override
public void run() {
//action
}
}.start();
}
});


I just wander if Handler can do it the "clean" way.

Answer

You can set up a Looper in a background thread using Looper.prepare() and Looper.loop.