Learn about the Android handler messaging mechanism _android

Source: Internet
Author: User

Android only allows UI threads to modify the UI components in the activity. When the Android program first starts, Android starts a main thread at the same time, the main thread is primarily responsible for handling UI-related events, such as user key events, screen drawing events, and distributing related events to the corresponding components for processing. Therefore, the main thread is often called the UI thread.

Android only allows the UI thread to modify the UI components in the activity, which can cause the newly started thread to not dynamically change the property values of the interface component. However, in the actual development of Android program, especially in the game development involving animation, it is necessary to make the new starting thread periodically change the attribute value of the interface component, which needs the help of the handler message passing mechanism.

1 Handler Class Introduction
The main functions of the handler class are two:
(1) Sending a message in a newly launched thread
SendMessage (msg) or
Sendemptymessage (int what)
The difference between the two, please see the Android source code:

Public Final Boolean SendMessage (Message msg) 
{return 
sendmessagedelayed (msg, 0); 
} 
Public final Boolean sendemptymessage (int what)
{return 
sendemptymessagedelayed (what, 0);
 

Look at the source code of sendemptymessagedelayed (what, 0):

Public final Boolean sendemptymessagedelayed (int what, long Delaymillis) 
{ 
msg = Message.obtain ();
Msg.what = what; 
Return sendmessagedelayed (msg, delaymillis); 

In fact, SendMessage (Message msg) and sendemptymessage (int what) are actually the same, a type of MSG, an int type of what, what, and eventually to Msg.

(2) Getting and processing messages in the main thread
Public void Handlemessage (msg)

2 Use instances
The following uses the handler and the timer class to implement the automatic refresh of the time

public class Mainactivity extends activity {@Override protected void onCreate (Bundle savedinstancestate) {Super.on
 Create (savedinstancestate);
 Setcontentview (R.layout.activity_main);
 Final TextView txt= (TextView) Findviewbyid (r.id.showtime); 

   Final Handler myhandler=new Handler () {@Override public void Handlemessage (msg) {if (msg.what==0x12) {

  Txt.settext ("Current time:" +new java.util.Date ());
 }
  }
 };
 Button btn= (button) Findviewbyid (R.ID.BTN); Btn.setonclicklistener (New Onclicklistener () {@Override public void OnClick (View arg0) {//TODO auto-generated met Hod stub new Timer (). Schedule (new TimerTask () {@Override public void run () {//TODO auto-generated method Stu
   b myhandler.sendemptymessage (0x12);  
  }}, 0,1000);

 }
 }); @Override public boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu; This adds the items to the Action bar I
 F it is present.
 Getmenuinflater (). Inflate (R.menu.main, menu);
 return true;
 
}
} 
 

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.