Talk Android together now (one hundred and seventy third back: Android's Handler Mechanism II)

Tell me what you were Hello, everybody, let's say on a gyrus is an example of Android in the Handler mechanism, this time we went to this example. Gossip Hugh mentioned, words Reformed turn. Let's Talk Android now!

Tell me who, in the last chapter we introduced the knowledge Handler's gyrus, gyrus that we will introduce a code via text combined with the use Handler.

  • 1. Create an App project, the project contains only a man named MainActivity empty Activity;
  • 2. Handler class defines SubHandler subclass, and override handleMessage method;
 public class SubHandler extends Handler {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case MSG_START:
                    showMessage(msg.obj.toString());
                    break;
                case MSG_END:
                    showMessage(msg.obj.toString());
                    break;
                default:
                    showMessage(msg.obj.toString());
                    break;
            }
        }
    }
  • 3. Define Object Handler type in MainActivity onCreate function of:
 mSubHandler = new SubHandler();
  • 4. Create two Button in MainActivity, and the listener is provided for transmitting messages;
mBTStartMsg = (Button) findViewById(R.id.id_bt_start_message);
mBTEndMsg = (Button) findViewById(R.id.id_bt_end_message);

mBTStartMsg.setOnClickListener(v ->sendMsg(MSG_START,"Start"));
mBTEndMsg.setOnClickListener(v -> sendMsg(MSG_END,"End"));
  • The definition of a method for transmitting a message, send the message to the message queue by this method;
 public void sendMsg(int type,String content) {
    Message msg = new Message();
    msg.what = type;
    msg.obj =content;
   mSubHandler.sendMessage(msg);
}
  • 6.Lopper removed from the message queue of the message and the callback Handler handleMessage method, Looper This operation is done automatically, we do not need to operate;

We look at the method of transmitting a key message in step 5, the parameters of the method is the type and content of the message, encapsulated in these two process parameters as objects of type Message, then Handler的sendMessagesent to the message queue method; Further, we the type of message written constants, so in line with the project specifications.

Let us analyze in step handleMessage Method 2 rewritable We msg.whattype determines the message, different messages is then processed according to different types of treatment herein is relatively simple: the content of the message and by obtaining Toast displayed to the user; when we were clicking start和end按钮, it will pop up Toast to display "Start"和"End".content displayed here and add content exactly the same as when sending a message to us.

Handler so unknown to us and send messages have been processed. As for how the message queue is created, how to manage, we do not care. In addition, we are sending and processing messages in the code are in the main thread, the thread does not involve the child, they can realize themselves, or wait a minute, we have a message behind gyrus chapter in the sub-thread, in examples of message processing main thread.

Tell me, on Android Handler mechanism in case we described here, and there are any examples For, Let's hear next decomposition!

Published 528 original articles · won praise 131 · views 620 000 +

Guess you like

Origin blog.csdn.net/talk_8/article/details/105164270