更新界面:可运行VS消息 [英] Updating GUI: Runnables vs Messages

查看:132
本文介绍了更新界面:可运行VS消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要更新的其他线程的图形用户界面,基本上有两种主要的方法:

To update the GUI from other threads, there are basically two main approaches:

  1. 使用了java.lang.Runnable与任一方法:

  1. Use java.lang.Runnable with any of these methods:

Activity.runOnUiThread(Runnable)
View.post(Runnable)
View.postDelayed(Runnable, long)
Handler.post(Runnable)

  • 使用android.os.Message:

  • Use android.os.Message:

    Handler.sendMessage(Message) / Handler.handleMessage(Message)
    

  • 您也可以使用AsyncTask的,但我的问题是更侧重于用例更新一个非常简单的组件。让我们来看看它是如何将使用这两种方法实现:

    You can also use AsyncTask, but my question is more focused on the use case of updating a very simple component. Let's see how it would be done using both approaches:

    1. 使用的Runnable:

    1. Using Runnables:

    TextViev tv = ...;
    final String data = "hello";
    Runnable r = new Runnable() {
    
        @Override
        public void run(){
            tv.setText(data);
        }
    
    };
    //Now call Activity.runOnUiThread(r) or handler.post(r), ...
    

  • 使用消息:

  • Using messages:

    Message m = handler.obtainMessage(UPDATE_TEXT_VIEW, "hello");
    handler.sendMessage(m);
    
    //Now on handler implementation:
        @Override
        public void handleMessage(Message msg) {
            if(msg.what == UPDATE_TEXT_VIEW){
                String s = (String) msg.obj;
                tv.setText(data);
            } ... //other IFs?
        }
    

  • 恕我直言,消息是不是要走的路,因为:

    IMHO, Messages are not the way to go because:

    • 不容易理解新的非Android程序员(处理程序挂钩在施工期间它的线程)。
    • 在对象的有效载荷应Parcellable如果消息跨越进程边界。
    • 在消息被重复使用(容易出错,如果不妥善清理?)
    • 在该处理器具有双重作用(其发送的邮件,也处理它们)
    • 信息属性是公共的,但也提供了的getter / setter。

    在另一方面,的Runnable遵循公知的命令模式,而且更程序员友好性和可读性。

    On the other hand, Runnables follow the well known command pattern, and are more programmer-friendly and readable.

    那么,什么是使用消息的Runnable上的优势是什么?难道消息推入现代的Andr​​oid编程的背景是什么?请问有什么可以用,不能用的Runnable完成的消息吗?

    So what are the advantages of using Messages over Runnables? Are Messages pushed into the background in modern day Android programming? Is there anything you can do with Messages that can't be done with Runnables?

    在此先感谢。

    推荐答案

    我会说有使用信息之间的差别不大 VS一个的Runnable 。这将主要归结为个人的preference。为什么?查看源$ C ​​$ C,你会发现,张贴的Runnable 使用完全相同的消息传递机制。它只是附加了的Runnable 消息和发送。

    I would say there is little difference between using a Message vs a Runnable. It'll mostly boil down to personal preference. Why? Looking at the source code you'll find that posting a Runnable uses the same exact messaging mechanism. It simply attaches the Runnable to a Message and sends that.

    4.4.2源$ C ​​$ C

    public final boolean post(Runnable r) {
        return  sendMessageDelayed(getPostMessage(r), 0);
    }
    
    private static Message getPostMessage(Runnable r) {
        Message m = Message.obtain();
        m.callback = r;
        return m;
    }
    

    编号:<一href="http://grep$c$c.com/file/repository.grep$c$c.com/java/ext/com.google.android/android/4.4.2_r1/android/os/Handler.java#Handler.post%28java.lang.Runnable%29"相对=nofollow> grep的code - 处理器

    这篇关于更新界面:可运行VS消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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