Handler mainly accepts data sent by child threads and uses this data to update the UI with the main thread.
Handler can distribute message objects and Runnable objects to the main thread, and each handler instance is bound to create his line thread (typically in the main thread), which has two effects: (1): Arranging messages or runnable executing somewhere in a main thread, (2) Arranging an action to be executed in a different thread.
In the previous article, we talked about the implementation of multithreading, in which we followed an example to implement Runnable interface to achieve multithreading and handler use.
Ask a question: How do I get the program to update title in 5 seconds?
First of all, let's take a look at the people who are used to Java programming and how to write the program before they know handler usage, as shown in the following code:
public class Testactivity extends activity {
private int count = 1;
@Override
public void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
New Thread (New Titlethread ()). Start ();
}
Class Titlethread implements Runnable {
@Override
public void Run () {
while (true) {
Settitle ("Welcome to my Blog" + count++);
try {
Thread.Sleep (5000);
catch (Interruptedexception e) {
E.printstacktrace ();
}
}
}
}
@Override
public boolean Oncreateoptionsmenu (Menu menu) {
Getmenuinflater (). Inflate (R.menu.activity_main, menu);
return true;
}
}
However, when we execute the program and do not achieve the desired effect, Android introduces the special class of handler, which can be said to be a bridge between runnable and activity, so we just send the message in the Run method, and in handler To perform different tasks through different message.
So our revised code is as follows:
public class Mainactivity extends activity {
private int count = 1;
Private Handler handler=new Handler () {
public void Handlemessage (Android.os.Message msg) {
Switch (msg.what) {
Case 1:
Updatetitle ();
Break
Default
Break
}
}
};
@Override
public void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
New Thread (New Titlethread ()). Start ();
}
protected void Updatetitle () {
Settitle ("Welcome to my Blog" + count++);
}
Class Titlethread implements Runnable {
@Override
public void Run () {
while (true) {
Message message=new message ();
Message.what=1;
Handler.sendmessage (message);
try {
Thread.Sleep (5000);
catch (Interruptedexception e) {
E.printstacktrace ();
}
}
}
}
@Override
public boolean Oncreateoptionsmenu (Menu menu) {
Getmenuinflater (). Inflate (R.menu.activity_main, menu);
return true;
}
}
In this way, the 5s clock update header is implemented after the operation ....
In addition, the creation of a thread can also be implemented using the Timer class combined with TimerTask. The specific code is as follows:
public class Timeractivity extends activity {
private int count = 1;
Private Handler handler=new Handler () {
public void Handlemessage (Android.os.Message msg) {
Switch (msg.what) {
Case 1:
Updatetitle ();
Break
Default
Break
}
}
};
@Override
public void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
Timer timer=new timer ();
Timer.scheduleatfixedrate (New TimerTask () {
@Override
public void Run () {
Message message=new message ();
Message.what=1;
Handler.sendmessage (message);
}
}, 1000,5000);
}
protected void Updatetitle () {
Settitle ("Welcome to my Blog" + count++);
}
}