1, activity of the Runonuithread
TextView = (TextView) Findviewbyid (r.id.tv);
New Thread (New Runnable () {
@Override public
void Run () {
runonuithread (new Runnable () {
@Override Public
void Run () {
textview.settext (update UI);}}}
)
. Start ();
The Android Activity Runonuithread () method uses
2, Handler sendemptymessage ()
Package lib.com.myapplication;
Import Android.os.Handler;
Import Android.os.Message;
Import android.support.v7.app.AppCompatActivity;
Import Android.os.Bundle;
Import Android.widget.TextView;
public class Mainactivity extends Appcompatactivity {
private TextView TextView;
Handler Handler = new Handler () {
@Override public
void Handlemessage (msg) {
super.handlemessage ( msg);
Textview.settext ("UI updated");
}
;
@Override
protected void onCreate (Bundle savedinstancestate) {
super.oncreate (savedinstancestate);
Setcontentview (r.layout.activity_main);
TextView = (TextView) Findviewbyid (r.id.tv);
New Thread (New Runnable () {
@Override public
void Run () {
try {
thread.sleep);
} catch (I Nterruptedexception e) {
e.printstacktrace ();
}
Handler.sendemptymessage (2);
}
}). Start ();
}
3, Handler post ()
Package lib.com.myapplication;
Import Android.os.Bundle;
Import Android.os.Handler;
Import android.support.v7.app.AppCompatActivity;
Import Android.widget.TextView;
public class Mainactivity extends Appcompatactivity {
private TextView TextView;
Handler Handler = new Handler ();
@Override
protected void onCreate (Bundle savedinstancestate) {
super.oncreate (savedinstancestate);
Setcontentview (r.layout.activity_main);
TextView = (TextView) Findviewbyid (r.id.tv);
New Thread (New Runnable () {
@Override public
void Run () {
try {
thread.sleep);
} catch (I Nterruptedexception e) {
e.printstacktrace ();
}
Handler.post (New Runnable () {
@Override public
void Run () {
textview.settext ("UI updated");
}
) ;
}
}). Start ();
}
4, view Post ()
TextView = (TextView) Findviewbyid (r.id.tv);
New Thread (New Runnable () {
@Override public
void Run () {
try {
thread.sleep);
} catch (Interr Uptedexception e) {
e.printstacktrace ();
}
Textview.post (New Runnable () {
@Override public
void Run () {
textview.settext ("UI updated");
}
) ;
}
}). Start ();
Summarize:
1, in fact, the above four ways can be attributed to one way: handler for the communication between the Android thread.
2. Why does Android require UI operations only on the UI thread? Mainly in order to avoid multithreading caused by concurrency problems. The single threaded operation UI is safe.
The above is to introduce you to the Android Update UI method Summary of the relevant knowledge, hope to help everyone, if you have questions welcome to my message, small series will promptly reply to everyone!