Everyone knows that because of performance requirements, Android requires that the UI be updated only in the UI thread, and there are roughly 4 ways to update the UI in other threads, using four different ways to update a textview.
1. Use the handler message passing mechanism
Package com.example.runonuithreadtest;
Import android.app.Activity;
Import Android.os.Bundle;
Import Android.os.Handler;
Import Android.widget.TextView;
public class Mainactivity extends activity {
private TextView TV;
Handler Handler = new Handler ()
{public
void Handlemessage (Android.os.Message msg) {
if (msg.what==0x123)
{
Tv.settext ("Updated TextView");
}}; @Override
protected void onCreate (Bundle savedinstancestate) {
super.oncreate (savedinstancestate);
Setcontentview (r.layout.activity_main);
TV = (TextView) Findviewbyid (r.id.tv);
New Mythread (). Start ();
Class Mythread extends Thread
{
@Override public
void Run () {
//delay two seconds to update
try {
Thread.Sleep ();
} catch (Interruptedexception e) {
//TODO auto-generated catch block
e.printstacktrace ();
}
Handler.sendemptymessage (0x123);}}
2. Using the Asynctask asynchronous task (updating the UI can only be done in the OnPostExecute (String result) method)
Package com.example.runonuithreadtest;
Import android.app.Activity;
Import Android.os.AsyncTask;
Import Android.os.Bundle;
Import Android.widget.TextView;
public class Mainactivity extends activity {
private TextView TV;
@Override
protected void onCreate (Bundle savedinstancestate) {
super.oncreate (savedinstancestate);
Setcontentview (r.layout.activity_main);
TV = (TextView) Findviewbyid (r.id.tv);
New Yibu (). Execute ();
Class Yibu extends Asynctask<string, string, string>
{
@Override
protected string Doinbackground (String ... params) {
try {
thread.sleep;
} catch (Interruptedexception e) {
//TODO auto-generated catch block< C22/>e.printstacktrace ();
}
return null;
}
@Override
protected void OnPostExecute (String result) {
//TODO auto-generated the method stub
Tv.settext (" Updated TextView ");}}
}
3. Using the Runonuithread (action) method
Package com.example.runonuithreadtest;
Import android.app.Activity;
Import Android.os.Bundle;
Import Android.widget.TextView;
public class Mainactivity extends activity {
private TextView TV;
@Override
protected void onCreate (Bundle savedinstancestate) {
super.oncreate (savedinstancestate);
Setcontentview (r.layout.activity_main);
TV = (TextView) Findviewbyid (r.id.tv);
New Mythread (). Start ();
Class Mythread extends Thread
{
@Override public
void Run () {
runonuithread (new Runnable ()
{ @Override public
Void Run () {
//TODO auto-generated method stub
try {
//delay two seconds update
Thread.Sleep ( );
} catch (Interruptedexception e) {
e.printstacktrace ();
}
Tv.settext ("Updated TextView");}}
}
4. Using the handler post (Runnabel R) method
Package com.example.runonuithreadtest;
Import android.app.Activity;
Import Android.os.Bundle;
Import Android.os.Handler;
Import Android.widget.TextView;
public class Mainactivity extends activity {
private TextView TV;
@Override
protected void onCreate (Bundle savedinstancestate) {
super.oncreate (savedinstancestate);
Setcontentview (r.layout.activity_main);
TV = (TextView) Findviewbyid (r.id.tv);
Handler Handler = new Handler ();
Handler.post (New Runnable () {
@Override public
void Run () {
try {
//delay two seconds update
Thread.Sleep ( );
} catch (Interruptedexception e) {
e.printstacktrace ();
}
Tv.settext ("Updated TextView");}}
The above is the four kinds of Android asynchronous update UI way, hope to help everyone's learning.