Android UI Update
People who have done Android development have been confronted with the problem that as the requirements change, some of the portal interfaces will be subject to changes in UI, content, and jump interface, and there are several ways to implement UI updates.
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 (ms
g);
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 (2000);
catch (Interruptedexception 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 ();
}
To switch to the main thread in a child thread
New Thread (New Runnable () {
@Override public
void Run () {
logutil.d ("TTT 11111111111" + Thread.CurrentThread (). GetName ());
New Handler (Looper.getmainlooper ()). Post (new Runnable () {
@Override public
void Run () {
LOGUTIL.D ("TTT 55555555 "+ thread.currentthread (). GetName ());
}
);
LOGUTIL.D ("TTT 22222222222" + thread.currentthread (). GetName ());
LOGUTIL.D ("TTT 33333333333" + thread.currentthread (). GetName ());
LOGUTIL.D ("TTT 44444444444" + thread.currentthread (). GetName ());
}
). Start ();
Results
TTT 11111111111thread-155
ttt 22222222222thread-155
ttt 33333333333thread-155
44444444444thread-155
TTT 55555555main
Visible this way can quickly switch threads, from log log, switch to the main thread does not block child threads.
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.
Thank you for reading, I hope to help you, thank you for your support for this site!