Android中Handler-Looper的实现

发布于 2024-10-23 17:53:15 字数 334 浏览 1 评论 0原文

  1. 我有带有 Handler 的 Activity(UI 线程),
  2. 我启动新线程并创建 handler.post(new MyRunnable()) - (新工作线程)

Android 文档谈到 post 方法:“导致 Runnable r 添加到消息队列。可运行程序将在该处理程序所附加的线程上运行。”

附加到 UI 线程的处理程序。 android 如何在同一个 UI 线程中运行 runnable 而无需创建新线程?

是否将使用 handler.post() 中的 Runnable 创建新线程? 或者只是从 Runnable 子类调用 run() 方法?

  1. I have Activity with Handler (UI thread)
  2. I start new Thread and make handler.post(new MyRunnable()) - (new work thread)

Android documentation said about post method: "Causes the Runnable r to be added to the message queue. The runnable will be run on the thread to which this handler is attached."

Handler attached to UI thread.
How android can run runnable in the same UI thread without new thread creation?

Is new thread will be created using Runnable from handler.post()?
Or it's only run() method will be called from Runnable subclass?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

陌伤浅笑 2024-10-30 17:53:15

这是一个如何使用处理程序的粗略伪代码示例 - 我希望它有帮助:)

class MyActivity extends Activity {

    private Handler mHandler = new Handler();

    private Runnable updateUI = new Runnable() {
        public void run() {
            //Do UI changes (or anything that requires UI thread) here
        }
    };

    Button doSomeWorkButton = findSomeView();

    public void onCreate() {
        doSomeWorkButton.addClickListener(new ClickListener() {
            //Someone just clicked the work button!
            //This is too much work for the UI thread, so we'll start a new thread.
            Thread doSomeWork = new Thread( new Runnable() {
                public void run() {
                    //Work goes here. Werk, werk.
                    //...
                    //...
                    //Job done! Time to update UI...but I'm not the UI thread! :(
                    //So, let's alert the UI thread:
                    mHandler.post(updateUI);
                    //UI thread will eventually call the run() method of the "updateUI" object.
                }
            });
            doSomeWork.start();
        });
    }
}

Here's a rough pseudroidcode example of how to use handlers - I hope it helps :)

class MyActivity extends Activity {

    private Handler mHandler = new Handler();

    private Runnable updateUI = new Runnable() {
        public void run() {
            //Do UI changes (or anything that requires UI thread) here
        }
    };

    Button doSomeWorkButton = findSomeView();

    public void onCreate() {
        doSomeWorkButton.addClickListener(new ClickListener() {
            //Someone just clicked the work button!
            //This is too much work for the UI thread, so we'll start a new thread.
            Thread doSomeWork = new Thread( new Runnable() {
                public void run() {
                    //Work goes here. Werk, werk.
                    //...
                    //...
                    //Job done! Time to update UI...but I'm not the UI thread! :(
                    //So, let's alert the UI thread:
                    mHandler.post(updateUI);
                    //UI thread will eventually call the run() method of the "updateUI" object.
                }
            });
            doSomeWork.start();
        });
    }
}
小猫一只 2024-10-30 17:53:15

附加到 UI 线程的处理程序。

正确的。

android 如何在同一个 UI 线程中运行 runnable 而无需创建新线程?

任何线程,包括主应用程序(“UI”)线程,都可以在 Handler(或任何 View)上调用 post()。

Handler attached to UI thread.

Correct.

How android can run runnable in the same UI thread without new thread creation?

Any thread, including the main application ("UI") thread, can call post() on Handler (or on any View, for that matter).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文