关于Handler

官方文档 http://developer.android.com/reference/android/os/Handler.html 有进行解释

其实我本身很好奇asynctask是怎么实现的。

A Handler allows you to send and process Message and Runnable objects associated with a thread's MessageQueue. Each Handler instance is associated with a single thread and that thread's message queue. When you create a new Handler, it is bound to the thread / message queue of the thread that is creating it -- from that point on, it will deliver messages and runnables to that message queue and execute them as they come out of the message queue.

There are two main uses for a Handler: (1) to schedule messages and runnables to be executed as some point in the future; and (2) to enqueue an action to be performed on a different thread than your own.

Scheduling messages is accomplished with the post(Runnable), postAtTime(Runnable, long), postDelayed(Runnable, long), sendEmptyMessage(int), sendMessage(Message), sendMessageAtTime(Message, long), and sendMessageDelayed(Message, long) methods. The post versions allow you to enqueue Runnable objects to be called by the message queue when they are received; the sendMessage versions allow you to enqueue a Message object containing a bundle of data that will be processed by the Handler's handleMessage(Message) method (requiring that you implement a subclass of Handler).

When posting or sending to a Handler, you can either allow the item to be processed as soon as the message queue is ready to do so, or specify a delay before it gets processed or absolute time for it to be processed. The latter two allow you to implement timeouts, ticks, and other timing-based behavior.

When a process is created for your application, its main thread is dedicated to running a message queue that takes care of managing the top-level application objects (activities, broadcast receivers, etc) and any windows they create. You can create your own threads, and communicate back with the main application thread through a Handler. This is done by calling the same post or sendMessage methods as before, but from your new thread. The given Runnable or Message will then be scheduled in the Handler's message queue and processed when appropriate.

  Handler允许你通过MessageQueue发送消息或者是可运行的对象。一个handler只跟一个线程的消息队列相对应。一旦你创建了handler它便与当前进程绑定,它能够传递或者是执行消息或者是可执行对象。

  用处有两,1、执行将来的消息和可执行对象;2、或者是让代码在其他的线程被执行。

  可用方法有post(Runnable), postAtTime(Runnable, long), postDelayed(Runnable, long), sendEmptyMessage(int), sendMessage(Message), sendMessageAtTime(Message, long), and sendMessageDelayed(Message, long)。POST能让你执行可执行对象,而sendMessage则可以发送类似bundle的数据。当你发送这些东西的时候还能决定要什么时候执行这些消息和对象。

 

   关于handler的绑定,如果你想绑定本线程,那么直接NEW就行了,但是如果你想把NEW出来的handler绑定到你指定的子线程中,那么就必须在NEW的时候给一个参数LOOPER,例如:(代码来自 http://www.itzhai.com/android-the-role-and-use-of-handler.html )

HandlerThread handlerThread = new HandlerThread("hanlderThread");
    //必须先调用该类的start的方法,否则该获取的Looper对象为NULL
    handlerThread.start();
    MyHanlder myHandler = new MyHandler(handlerThread.getLooper());
    Message msg = myHandler.obtainMessage();
    Bundle bundle = new Bundle();
    bundle.putString("name","arthinking");

    msg.setData(b);
    msg.sendToTarget();

即可给指定的子线程发送message数据。当然获得了handler还能POST可执行的那些对象。

官方的AsyncTask就是通过handler来实现把结果交给UI线程处理的。

 

posted @ 2013-11-07 21:55  yutoulck  阅读(153)  评论(0编辑  收藏  举报