Android asynchronous message processing mechanism (1) Handler basic use

Source: Internet
Author: User

The Android UI is thread insecure, and if you try to do UI action in a child thread, the program may crash. The solution should be to create a message object, then send it with handler, and then get the message object just sent in the handler Handlemessage () method, and then the UI action here will no longer crash.
This processing is called an asynchronous message processing thread. The simplest is to implement the update UI operation in a child thread.

Handler Basic use

There are two types of handler used:

    • Sends a message from a child thread (worker thread) to the main thread (UI thread) and processes the message in the main thread;
    • Sends a message to a child thread (worker thread) from the main thread (the UI thread) and processes the message in a child thread.
A child thread sends a message to the main thread

The main steps are as follows:

    1. Customizing handler (non-abstract class) subclasses in the main thread and implementing handleMessage(Message) methods;
    2. A message is sent by means of the handler object in the worker thread sendMessage(Message) ;
    3. The handler object places the message in the messages queue messagqueue;
    4. Looper takes a message out of the messages queue and finds the corresponding handler object.
    5. Looper calls the handler object's handleMessage(Message) method to process the message.

Among them, we just complete 1, 22 steps, the rest is done by the system.

The main code is as follows:

//在main Thread中创建Handler对象new MyHandler();//创建worker Threadnew Thread(new Runnable() {    @Override    publicvoidrun() {    //创建消息对象        Message message = handler.obtainMessage();        "我是从worker Thread向main Thread发送的消息";        //发送消息对象        handler.sendMessage(message);    }}).start();

Among them, the MyHandler class is as follows:

class MyHandler extends Handler {    @Override    publicvoidhandleMessage(Message msg) {        Log.d("result:",Thread.currentThread().getName());        //只有在main Thread中才能操作UI        Log.d("result",(String)msg.obj);    }}

The above method enables the message to be sent from the child thread (worker thread) to the main thread (UI thread), and the message is processed in the main thread.

Mainline Cheng thread sending messages

The main steps are as follows:

1. 准备Looper对象2. 在 worker Thread生成Handler对象并实现`handleMessage()`方法;3. 在MainThread当中通过Handler对象的`sendMessage(Message)`方法发送Message。

The implementation code that is typically in a child thread is as follows:

new Thread(){    @Override    publicvoidrun() {        //准备Looper对象        Looper.prepare();        new MyHandler();        //循环检查消息队列,然后调用handleMessage()方法处理,若没有消息对象则该线程阻塞。        Looper.loop();    }}.start();

The above MyHandler class is still used here. The Handler2 method is then invoked directly in the main thread to sendMessage(Message) send a message to the child thread and process the message in the child thread. The code is as follows:

"我是从main Thread向worker Thread 发送的消息";handler2.sendMessage(message);

The method above implements sending a message from the main thread (UI thread) to a child thread (worker thread), processing the message in the main thread.

That is, where to create the handler object, where the object is processed.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Android asynchronous message processing mechanism (1) Handler basic use

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.