Hello everyone, this section is about the use of Android handler. before talking about handler, let's raise a small question: how can the program update the title in five seconds. first, let's take a look at the Java programmers who are used to writing programs before they know how to use handler. The Code is as follows:
- Package com. Android. tutor;
- Import java. util. timer;
- Import java. util. timertask;
- Import Android. App. activity;
- Import Android. OS. Bundle;
- Public class handlerdemoextends activity {
- // Title provides variables for the settitle method. I have set this variable to the int type for convenience.
- Private int title = 0;
- Public void oncreate (bundle savedinstancestate ){
- Super. oncreate (savedinstancestate );
- Setcontentview (R. layout. Main );
- Timer timer = new timer ();
- Timer. scheduleatfixedrate (New mytask (), 1,5000 );
- }
- Private class mytaskextends timertask {
- @ Override
- Public void run (){
- Settitle ("welcome to Mr Wei's blog" + title );
- Title ++;
- }
- }
- }
Package COM. android. tutor; import Java. util. timer; import Java. util. timertask; import android. app. activity; import android. OS. bundle; public class handlerdemo extends activity {// Title provides variables for the settitle method. For convenience, I have set the int type private int title = 0; Public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); timer = new timer (); timer. scheduleatfixedrate (New mytask (), 1, 5000);} private class mytask extends timertask {@ overridepublic void run () {settitle ("welcome to Mr Wei's blog" + title); Title ++ ;}}}However, when we execute a program, we cannot achieve the expected results. Therefore, Android introduces the special class handler, which can be said to be a bridge between runnable and activity, therefore, we only need to send a message in the run method, and execute different tasks through different messages in handler. The modified code is as follows:
- Package com. Android. tutor;
- Import java. util. timer;
- Import java. util. timertask;
- Import Android. App. activity;
- Import Android. OS. Bundle;
- Import Android. OS. Handler;
- Import Android. OS. message;
- Public class handlerdemoextends activity {
- // Title provides variables for the settitle method. I have set this variable to the int type for convenience.
- Private int title = 0;
- Private handler mhandler = new handler (){
- Public void handlemessage (Message MSG ){
- Switch (msg. What ){
- Case 1:
- Updatetitle ();
- Break;
- }
- };
- };
- Public void oncreate (bundle savedinstancestate ){
- Super. oncreate (savedinstancestate );
- Setcontentview (R. layout. Main );
- Timer timer = new timer ();
- Timer. scheduleatfixedrate (New mytask (), 1,5000 );
- }
- Private class mytaskextends timertask {
- @ Override
- Public void run (){
- Message message = new message ();
- Message. What = 1;
- Mhandler. sendmessage (Message );
- }
- }
- Public void updatetitle (){
- Settitle ("welcome to Mr Wei's blog" + title );
- Title ++;
- }
- }