Android handler, activity, and service Summary

Source: Internet
Author: User
 

1. Handler Learning

Because of the time-consuming operations that sometimes need to be performed, if these operations are placed in the main thread, it is easy to cause the program to be suspended, and the final result is a system crash. Therefore, to perform some time-consuming operations, place them in the sub-thread. This involves how to solve the communication problem between the sub-thread and the main thread (Message Queue ).

Handler exists to solve this problem. When you process a time-consuming operation, you put it in the sub-thread. After the sub-thread completes processing, the result is placed in the message queue, then, the main thread reads the processing results of the subthread from the message queue, and updates the main thread based on the results. In this way, Handler processes the entire process.

Handler can communicate with sub-threads in two ways:

1. Add the runnable object to the Message Queue through post (runnable object), then the main thread reads the runnable object from the message queue, and updates the main thread based on the result.

Package COM. test. ui; import android. app. activity; import android. OS. handler; import android. OS. bundle; import android. OS. message; import android. view. view; import android. widget. button; import android. widget. progressbar; public class testactivity extends activity {/** called when the activity is first created. */button btn1, btn2; progressbar progress; int TMP = 0; handler = new handler (); Public Void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); btn1 = (button) findviewbyid (R. id. button1); btn2 = (button) findviewbyid (R. id. button2); Progress = (progressbar) findviewbyid (R. id. progressbar1); btn1.setonclicklistener (new view. onclicklistener () {// button control event listener @ override public void onclick (view v) {handler. post (update); // use the post function to set the runna Ble object added to message queue, update SS Based on Message results});} runnable update = new runnable () {// create a subthread to handle time-consuming operations, then update SS in the main thread. @ Override public void run () {If (Progress. getprogress () <100) {TMP + = 10; progress. setprogress (TMP); Progress. setsecondaryprogress (TMP + 10); handler. postdelayed (Update, 800); // after a delay of 0.8 seconds, add the runnable object to the Message Queue.} else {handler. removecallbacks (update); // undo the runnable object SS from the message. setvisibility (view. gone); // tmp ss invisible TMP = 0 ;}}};}

2. The second handler communication method is to send messages to the Message Queue through the subthread sendmessage function. Then, the main thread reads the message in the message queue by overwriting handlermessage, and then updates the main thread.

Function call process:

Add the runnable object to the Message Queue through post (), run the run function of the runnable object, and call the handlermessage function in the main thread to update the UI after running the sendmessage function, if the UI update is complete, use removecallb
Acks removes the runnable object from the queue.

Package COM. test. ui; import android. app. activity; import android. OS. handler; import android. OS. bundle; import android. OS. message; import android. view. view; import android. widget. button; import android. widget. progressbar; public class testactivity extends activity {private button btn1; private progressbar progress; int TMP = 0; handler = new handler () {@ overridepublic void handlemessage (Message MSG) {Progress. setprogress (MSG. arg1); If (MSG. arg1 = 100) {handler. removecallbacks (update); // The update is complete, and the runnable object is removed from the progress. setvisibility (view. gone); TMP = 0;} elsehandler. post (update); // The update is not complete, and the thread to be executed is put into the queue.}; Public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); btn1 = (button) findviewbyid (R. id. button1); Progress = (progressbar) findviewbyi D (R. id. progressbar1); btn1.setonclicklistener (new view. onclicklistener () {// event listening public void onclick (view v) {// set the progress bar to the visible status progress. setvisibility (view. visible); handler. post (update) ;}}) ;}runnable update = new runnable () {// create a subthread to handle time-consuming operations and then update progress in the main thread. Public void run () {TMP = TMP + 10; message MSG = handler. obtainmessage (); MSG. arg1 = TMP; try {thread. sleep (800); // sleep the current thread for 800 milliseconds} catch (interruptedexception ex) {ex. printstacktrace ();} handler. sendmessage (MSG); // send the message to the message queue, waiting for the main thread to call the handlermessage function for processing (post-processing after run) if (TMP = 100) {handler. removecallbacks (update); // The update is complete. revoke the runnable object TMP = 0 ;}}};}

2. Activity Learning

3. Service Learning

4. Message Learning

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.