【问题标题】:Working of Handlers and Asynctask in android?处理程序和异步任务在android中的工作?
【发布时间】:2012-03-23 03:49:30
【问题描述】:

我想知道是否有人可以解释以下教程的控制流程:
http://www.vogella.de/articles/AndroidPerformance/article.html

我不知道 Handler 的 Runnable 和 post() 方法是如何工作的?

谢谢

斯内哈

【问题讨论】:

    标签: android multithreading android-asynctask handler


    【解决方案1】:

    示例 1:

    Handler threadHandler=new Handler();
            threadHandler.postDelayed(new Runnable() {
                public void run() {
                    // do your task here ..it will execute after 100 ms in separate thread 
                }
            }, 100L);
    

    示例 2:

         final Handler mHandler = new Handler(Looper.getMainLooper());
                            new Thread(){
                                /* (non-Javadoc)
                                 * @see java.lang.Thread#run()
                                 */
                                @Override
                                public void run() {
        // do your task here.. it will execute in seperate thread
    // you can post your result from here using mHandler.post Method.
                            }       
                            }.start();
    

    互联网上有很多教程。你可以很容易地找到它。

    看到这个:

    http://mindtherobot.com/blog/159/android-guts-intro-to-loopers-and-handlers/

    【讨论】:

      【解决方案2】:

      Runnable 将可运行的代码块表示为 Object,handler 中的 post 方法需要一个可运行的对象作为输入,handler 执行该对象。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多