Sub thread to update main thread (UI) 2
Handler. Post (somethread );
Handler. sendmessage ("MSG ");
1 import android. app. activity; 2 Import android. OS. bundle; 3 Import android. OS. handler; 4 Import android. OS. message; 5 import android. util. log; 6 Import android. view. view; 7 Import android. widget. button; 8 Import android. widget. progressbar; 9 10 public class handleractivity extends activity {11 12 button btnstart, btnend; 13 progressbar probar; 14 15/** called when the activity is first created. */16 @ override17 public void oncreate (bundle savedinstancestate) {18 super. oncreate (savedinstancestate); 19 setcontentview (R. layout. main); 20 21 // The control object 22 btnstart = (button) findviewbyid (R. id. start); 23 btnend = (button) findviewbyid (R. id. end); 24 probar = (progressbar) findviewbyid (R. id. pbar); 25 26 // the start button triggers event 27 btnstart. setonclicklistener (new view. onclicklistener () {28 29 @ override30 public void onclick (view v) {31 probar. setvisibility (view. visible); 32 <span style = "color: # ff0000;"> updatebarhandler. post (update0000read); </span> 33} 34}); 35 36 // The end button triggers the event 37 btnend. setonclicklistener (new view. onclicklistener () {38 39 @ override40 public void onclick (view v) {41 <span style = "color: # ff0000;"> updatebarhandler. removecallbacks (updatedomainread); </span> 42} 43}); 44} 45 46 47 // create a handler object 48 handler updatebarhandler = new handler () {49 50 @ override51 public void handlemessage (Message MSG) {52 probar. setprogress (MSG. arg1); 53 54 // repeat55 updatebarhandler. post (updateprohibited read); 56} 57 58}; 59 60 // update the progressbar thread object 61 runnable updateprohibited READ = new runnable () {62 int I = 0; 63 @ override64 public void run () {65 I = I + 10; 66 message MSG = updatebarhandler. obtainmessage (); 67 MSG. arg1 = I; 68 try {69 thread. sleep (2000); 70} catch (interruptedexception e) {71 E. printstacktrace (); 72} 73 updatebarhandler. sendmessage (MSG); 74 if (I = 100) {75 updatebarhandler. removecallbacks (update0000read); 76} 77} 78}; 79} 80
Explanation:
The general process of using handler:
1. Create a handler object. You can use the handler no-argument constructor to create a handler object, inherit the hander class, and override the handlemessage method to create a handler object.
2. In the listener, call the POST method of handler and add the thread object to be executed to the thread queue. At this time, the thread object will be added to the thread queue of the handler object.
3. Write the operation to be executed in the run method of the thread object. Generally, a runnable object can be used to re-write the run method.
Handler contains two queues, one of which is thread queue and the other is message queue. The post method puts the thread object in the thread queue of the handler and the message queue using sendmessage.
If you want to keep executing this process, you can execute the postdelayed or POST method within the run method, and then add the thread object to the Message Queue for repeated execution. To stop the thread execution, call the removecallbacks method of the handler object and remove the thread object from the thread queue.
Handler provides an asynchronous message processing mechanism for Android. When a message (sendmessage) is sent to a message queue, it returns immediately, and the reading of the message from the message queue is blocked, when reading a message in a message queue, the public void handlemessage method in handler is executed. Therefore, when creating a handler, you should use an anonymous internal class to override this method, write the operation after reading the message in this method, and use handler's obtainmessage () to obtain the message object.
The relationship between handler and thread:
After the POST method of handler is used to put the runable object in the thread queue of handler, the execution of the runnalbe does not actually enable the thread separately, but is still executed in the current activity thread, handler only calls the run method of the runable object.
How to enable a new thread when handler executes runnable:
1. Generate a handlerthread object to implement the function of using logoff to process message queues. This class is provided by the android application architecture.
Handlerthread = new handlerthread ("handler_thread ");
2. Before using the getlooper () method of handlerthread, you must call start () of the class ();
3. Obtain the logoff object based on the handlerthread object.
4. Create a custom subclass that inherits from the handler class, and implement a construction method of The logoff object parameter. The method content can call the constructor of the parent class.
5. use the logoff object obtained in step 3 to create a Custom Handler subclass object, and then send the message to the handler message queue. handlemessage () is rewritten by handler () will be executed to process messages in the message queue.
A message, that is, a message object, can transmit some information. You can use arg1, arg2, and object to transmit some integer or object information. You can also use setdata (bundle) of the message object) to pass the bundle object to the newly created thread. When executing the handlemessage, the newly created thread can use getdata () to extract the bundle object for processing from the message.
Sub thread to update main thread (UI) 2