Chapter 4 three cornerstones of Android development: Activity, Service, Handler (6), and androidhandler
4.3 Communication Between Android threads-Message Mechanism
Messages are often used during information interaction between threads or threads in Android programs. If we are familiar with these basic things and their internal principles, it will make our Android development easier, so as to better structure the system. Next, let's take a look at what the message mechanism in Android is like.
4.3.1 message transmission-Handler usage
In Android, messages are often used for information interaction between threads. When is "message" used? How does it work?
Before explaining this, let's take a look at the following Activity:
|
// Import omitted
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 ();
}
// Working thread
Private class WorkThread extends Thread {
@ Override
Public void run (){
// Process time-consuming operations here
// Changes the status after processing.
StateText. setText ("completed ");
}
}
} |
In the above Activity, it seems quite normal, a TextView and a Button. However, an error will be reported immediately after you click the button:
|
ERROR/AndroidRuntime (3658): android. view. ViewRoot $ CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views |
------------------------------------------ Try to put an advertisement. Now there is no job to survive. There is no way. The annual ROI of the p2p platform affiliated to Ping An group is 7%-9%. This is the preferred personal experience to replace bank financial management. the Rainbow Project should not invest in security. That is almost impossible to transfer. If you want to withdraw cash in advance, it is very difficult to link to http://affiliate.lufax.com/action/36xbu's first investment of Yuan, that is, a few hundred yuan in the quota --------------------------------------------
What is going on? The reason is that the view component in the Android system is not thread-safe. to update a view, the view must be updated in the main thread. The update operation cannot be performed in the Child thread.
In this case, we will notify the main thread in the Child thread and let the main thread perform the update operation. So how do we notify the main thread? We need to use the Handler object.
Let's slightly modify the above Code:
|
// Import omitted
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 ();
}
// Working thread
Private class WorkThread extends Thread {
@ Override
Public void run (){
// Process time-consuming operations here
Message msg = new Message ();
Msg. what = COMPLETED;
Handler. sendMessage (msg );
}
}
} |
In this way, we can solve the thread security problem by handing over the complex task processing work to the subthread for completion, and then the subthread notifies the main thread through the Handler object, the message mechanism plays an important role in updating the view by the main thread.