SoFunction
Updated on 2025-04-07

Learn Android Handler messaging mechanism

Android only allows UI threads to modify UI components in the Activity. When the Android program is launched for the first time, Android will start a main thread (Main Thread) at the same time. The main thread is mainly responsible for handling UI-related events, such as user key events and screen drawing events, and distributing related events to the corresponding components for processing. Therefore, the main thread is usually also called a UI thread.

Android only allows UI threads to modify UI components in the Activity, which will cause the newly started thread to be unable to dynamically change the attribute value of the interface component. However, in actual Android program development, especially in game development involving animation, the newly launched thread needs to periodically change the attribute value of the interface component, which needs to be achieved with the help of Handler's messaging mechanism.

1 Introduction to Handler Class
The Handler class has two main functions:
(1) Send a message in the newly started thread
sendMessage(Message msg) or
sendEmptyMessage(int what)   
For 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);
 
} 

Let's look at the source code of sendEmptyMessageDelayed(what, 0):

public final boolean sendEmptyMessageDelayed(int what, long delayMillis) 
{ 
Message msg = ();
 = what; 
return sendMessageDelayed(msg, delayMillis); 
}

In fact, sendMessage(Message msg)  and sendEmptyMessage(int what)  are actually the same. One passes to the Message type msg, the other passes to the int type what, and the one passes to the What, will eventually be converted to msg.

(2) Get and process messages in the main thread
 public void handleMessage(Message msg) 

2 Examples
The following uses Handler and Timer classes to achieve automatic refresh time

public class MainActivity extends Activity {
 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 (savedInstanceState);
 setContentView(.activity_main);
 final TextView txt=(TextView)findViewById();
 final Handler myHandler=new Handler()
 {
  @Override
  public void handleMessage(Message msg)
  {
  if(==0x12)
  {

   ("Current time:"+new ());

  }
  }
 };
 Button btn=(Button)findViewById();
 (new OnClickListener() {
  @Override
  public void onClick(View arg0) {
  // TODO Auto-generated method stub

  new Timer().schedule(new TimerTask() {
  @Override
   public void run() {
   // TODO Auto-generated method stub

   (0x12);
   }
  }, 0,1000);  
  }
 });

 }
 

 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
 // Inflate the menu; this adds items to the action bar if it is present.
 getMenuInflater().inflate(, menu);
 return true;
 }
 

} 

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.