Four Ways for Android to Update UI Asynchronous
- 2021-07-13 06:10:31
- OfStack
As we all know, due to performance requirements, android requires that UI can only be updated in UI threads. There are roughly four ways to update UI in other threads, and the following four ways are used to update one TextView.
1. Use the Handler messaging 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 updates by two seconds
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
handler.sendEmptyMessage(0x123);
}
}
}
2. Use the AsyncTask asynchronous task (update UI only 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(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
tv.setText(" Updated TextView");
}
}
}
3. Use 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 updates by two seconds
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
tv.setText(" Updated TextView");
}
});
}
}
}
4. post (Runnabel r) method using Handler
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 updates by two seconds
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
tv.setText(" Updated TextView");
}
});
}
}
These are four ways for Android to update UI asynchronously, hoping to help everyone's study.