在后台线程中运行处理程序的消息 [英] Run Handler messages in a background thread

查看:84
本文介绍了在后台线程中运行处理程序的消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

这篇关于在后台线程中运行处理程序的消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆