在后台线程中运行处理程序的消息

在后台线程中运行处理程序的消息

问题描述:

我要运行一些可运行在后台线程。我想用处理程序,因为它的方便延迟。 我的意思是

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。我知道如何与自定义类做扩展线程,但它需要多一点的编码工作不是做它的处理方式。所以,请不要张贴其他的解决方案或类似的东西。

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.

您可以通过设置一个活套在后台线程活套。prepare() Looper.loop

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