The Android UI action is thread insecure. We can only modify the UI in the UI thread, or in the main thread. Imagine multiple thread operations on the same UI, which can cause inconsistencies. The main work of the UI thread is the UI update display, the interaction of individual controls, and so on. Some time-consuming (time-consuming) operations cannot be placed in the UI thread, typically such as querying a database, network requests, and so on. These actions are left to the worker thread to do. How do I display the worker thread's work results on the UI?
Android provides a workaround: Provide handler for communication between thread. Send a message via handler or runnable to Messagequeen. There are a lot of concepts here. These concepts are key to understanding Android Thread.
First, implement the worker thread to modify the UI.
The worker Thread is relative to the main thread, main thread (UI Thread).
With a simple example description: Click the button screen toast prompt to update the button text.
1. Send a message to Messagequeen via handler. Complete the UI update in Handler's callback function, Handlermessage ().
public class Mainactivity extends Activity {private Handler mhandler;private Button Mbutton; @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); Mbutton = (Button) Findviewbyid (r.id.btn), Mhandler = new Handler () {@Overridepublic void Handlemessage (Message msg) { UpdateUI ();};}; Mbutton.setonclicklistener (New Onclicklistener () {@Overridepublic void OnClick (View arg0) {//TODO auto-generated Method Stubnew Thread (new Runnable () {@Overridepublic void Run () {//TODO auto-generated method Stubtimeconsumetask (); Mhandler.sendemptymessage (0);}}). Start ();} }); }
Update UI Operations public void UpdateUI () {Toast.maketext (Getapplicationcontext (), "Update UI", Toast.length_long). Show (); Mbutton.settext ("UI update"); }
Simulation time-consuming operation public void Timeconsumetask () {try {Thread.Sleep]} catch (Interruptedexception e) {//TODO Auto-gene Rated catch Blocke.printstacktrace ();} }}
Mhandler is the activity's member variable, and the update UI action in the Handlemessage function is done in the UI thread.
Show:
2. Through handler to the Post Runnable object in Messagequeen, the UI update operation is performed in the Runnable object.
Make a slight change to the code above:
public class Mainactivity extends Activity {private Handler mhandler;private Button Mbutton; @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); Mbutton = (Button) Findviewbyid (r.id.btn)//mhandler = new Handler () {//@Override//public void Handlemessage (Message msg ) {//updateui ();//};//}; Mhandler = new Handler (); Mbutton.setonclicklistener (New Onclicklistener () {@Overridepublic void OnClick (View arg0) {//TODO auto-generated Method Stubnew Thread (new Runnable () {@Overridepublic void Run () {//TODO auto-generated method Stubtimeconsumetask ();// Mhandler.sendemptymessage (0); Mhandler.post (new Runnable () {@Overridepublic void Run () {//TODO auto-generated method Stubupdateui ();}}). Start ();} }); } public void UpdateUI () {Toast.maketext (Getapplicationcontext (), "Update UI", Toast.length_long). Show (); Mbutton.settext ("UI update"); } public void Timeconsumetask () {try {Thread.Sleep],} catch (Interruptedexception e) {//TODO auto-generated catch Blocke.printstacktrace ();} }}
The result of the operation is the same.
The Mhandler post Runnable object is in the UI thread's Messagequeen, and the UI thread's Looper is responsible for handling the update UI code in Messagequeen in Message,runnable and ultimately in the UI Thread to execute.
Use of Android Handler