We often do time-consuming operations in a background thread, such as going to the network to fetch data. But when the data is retrieved and needs to be displayed on the page, there are some minor problems, as we all know that the Android UI page is not allowed to operate directly on other threads. The following is a summary of the 4 methods used to manipulate the UI interface in a thread.
Simulate time-consuming operations
Private void throws interruptedexception { thread.sleep);}
Method One: Handler
The event is sent through the handler SendMessage (msg) in the child thread:
Private voidmethod1 () {NewThread (NewRunnable () {@Override Public voidrun () {Try { //time-consuming Operationconnectnet (); //send a message to handlerMhadndler.sendemptymessage (111); } Catch(interruptedexception e) {e.printstacktrace (); }}). Start ();}
Method Two: View.post (Runnable)
Private voidmethod2 () {Mytext.setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (View v) {NewThread (NewRunnable () {@Override Public voidrun () {Try { //time-consuming Operationconnectnet (); Mytext.post (NewRunnable () {@Override Public voidrun () {Mytext.settext ("End of networking, update UI data"); } }); } Catch(interruptedexception e) {e.printstacktrace (); }}). Start (); } });}
Method Three: Activity.runonuithread
Similar to View.post
Private voidmethod3 () {NewThread (NewRunnable () {@Override Public voidrun () {Try { //time-consuming Operationconnectnet (); Runonuithread (NewRunnable () {@Override Public voidrun () {Mytext.settext ("Runonuithread ..."); } }); } Catch(interruptedexception e) {e.printstacktrace (); }}). Start ();}
method Four: Asynctask
Private voidmethod4 () {//executing in the UI thread NewLoadtask (). Execute ("www.91dota.com");}Private classLoadtaskextendsAsynctask {protected voidOnPostExecute (String result) {Mytext.settext (result);//get information from the Network Refresh page } protectedString Doinbackground (object[] objects) {return"......";//background time-consuming operations }}
=========================== "
Attach code and layout
Mainactivity.java
Public classMainactivityextendsappcompatactivity {PrivateTextView MyText; PrivateHandler Mhadndler =NewHandler () {@Override Public voidhandlemessage (Message msg) {Super. Handlemessage (msg); Mytext.settext ("Get the message ..." +msg.what); } }; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); MyText=Findviewbyid (R.id.mytextview); } /*** Method One: Handler*/ Private voidmethod1 () {NewThread (NewRunnable () {@Override Public voidrun () {Try { //time-consuming Operationconnectnet (); //send a message to handlerMhadndler.sendemptymessage (111); } Catch(interruptedexception e) {e.printstacktrace (); }}). Start (); } /*** Method Two: Through View.post (Runnable)*/ Private voidmethod2 () {Mytext.setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (View v) {NewThread (NewRunnable () {@Override Public voidrun () {Try { //time-consuming Operationconnectnet (); Mytext.post (NewRunnable () {@Override Public voidrun () {Mytext.settext ("End of networking, update UI data"); } }); } Catch(interruptedexception e) {e.printstacktrace (); }}). Start (); } }); } /*** Method Three: Activity.runonuithread (Runnable)*/ Private voidmethod3 () {Runonuithread (NewRunnable () {@Override Public voidrun () {Try { //time-consuming Operationconnectnet (); Mytext.settext ("Runonuithread ..."); } Catch(interruptedexception e) {e.printstacktrace (); } } }); } /*** Method Four: Asynctask*/ Private voidmethod4 () {//executing in the UI thread NewLoadtask (). Execute ("www.91dota.com"); } Private classLoadtaskextendsAsynctask {protected voidOnPostExecute (String result) {Mytext.settext (result);//get information from the Network Refresh page } protectedString Doinbackground (object[] objects) {return"......";//background time-consuming operations } } Private voidConnectNet ()throwsinterruptedexception {thread.sleep (2000); }}
Activity_main.xml
<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:app= "Http://schemas.android.com/apk/res-auto"Xmlns:tools= "Http://schemas.android.com/tools"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "vertical"> <TextViewAndroid:id= "@+id/mytextview"Android:layout_width= "Match_parent"Android:layout_height= "50DP"Android:layout_margin= "10DP"Android:background= "#dea"android:gravity= "Center"Android:text= "Hello world!" /></LinearLayout>
Reference: Http://www.it165.net
Some ways Android operates the UI thread