4.3 Communication between Android threads-messaging mechanism
In the case of Android programs, messages are often used when interacting between threads or within threads, and if we are familiar with these basics and their internal principles, it will make our Android development much easier and thus better architected the system. Below, let's look at the message mechanism in Android what's going on.
4.3.1 The use of-handler message delivery
In Android, messages are often used when information is interacting between threads, so when does "message" come into use? How does it work?
Before explaining this, let's take a look at the following activity:
Import slightly public class Mainactivity extends Activity implements View.onclicklistener { Private TextView Statetext; Private Button btn; @Override public void OnCreate (Bundle savedinstancestate) { Super.oncreate (savedinstancestate); Setcontentview (R.layout.message); Statetext = (TextView) Findviewbyid (R.id.my_text); BTN = (Button) Findviewbyid (R.ID.MY_BTN); Btn.setonclicklistener (this); } @Override public void OnClick (View v) { New Workthread (). Start (); } Worker threads Private class Workthread extends Thread { @Override public void Run () { Handle more time-consuming operations here Change status after processing is complete Statetext.settext ("completed"); } } } |
In the above activity, I see, quite normal, a textview, a button. However, clicking the button immediately after the run will cause an error:
Error/androidruntime (3658): Android.view.viewroot$calledfromwrongthreadexception:only The original thread that Created a view hierarchy can touch its views |
--------------------------------------------trying to put an ad, and now there's no work to survive.Ping An Lufax is affiliated to the Ping An group's peer platformannual ROI 7%-9% is the preferred alternative to bank bankingPersonal lessons recommend investing in anxin or guaranteed rainbow projectsdon't invest in an e that's almost impossible to transfer it's very difficult to withdraw in advancesite LinksHttp://affiliate.lufax.com/action/36XBUThe first investment of 1000 yuan to make an additional hundreds of Yuan--------------------------------------------
What the hell is going on here? The reason is that the view components in the Android system are not thread-safe, and if you want to update the view, you must update it in the main thread, and you cannot perform the updated operation on the child thread.
In this case, we will notify the main thread in the child threads, let the main thread do the update operation. So how do we notify the main thread? We need to use the handler object.
Let's modify the above code a little bit:
Import slightly public class Mainactivity extends Activity implements View.onclicklistener { Private TextView Statetext; Private Button btn; private static final int completed = 1; Private Handler Handler = new Handler () { @Override public void Handlemessage (Message msg) { if (Msg.what = = completed) { Statetext.settext ("completed"); } } }; @Override public void OnCreate (Bundle savedinstancestate) { Super.oncreate (savedinstancestate); Setcontentview (R.layout.message); Statetext = (TextView) Findviewbyid (R.id.my_text); BTN = (Button) Findviewbyid (R.ID.MY_BTN); Btn.setonclicklistener (this); } @Override public void OnClick (View v) { New Workthread (). Start (); } Worker threads Private class Workthread extends Thread { @Override public void Run () { Handle more time-consuming operations here Message msg = new Message (); Msg.what = completed; Handler.sendmessage (msg); } } } |
In this way, we can solve the problem of thread safety, the complex task processing to the child thread to complete, and then the child thread through the handler object to inform the main thread, the main thread to update the view, the process of the message mechanism plays an important role.
Fourth three cornerstones of Android development-activity, service and handler (6)