Four ways to update UI asynchronously on Android

Four ways to update UI asynchronously on Android

As we all know, due to performance requirements, Android requires that the UI can only be updated in the UI thread. To update the UI in other threads, I have roughly summarized 4 ways. You are welcome to add corrections:

[[147881]]

  1. Use the Handler message passing mechanism;

  2. Use AsyncTask asynchronous task;

  3. Use runOnUiThread(action) method;

  4. Use the post(Runnabel r) method of Handler;

The following four methods are used to update a TextView.

1. Use Handler message passing mechanism

  1. package com.example.runonuithreadtest;
  2. import android.app.Activity;
  3. import android.os.Bundle;
  4. import android.os.Handler;
  5. import android.widget.TextView;
  6. public   class MainActivity extends Activity {
  7. private TextView tv;
  8. Handler handler = new Handler()
  9. {
  10. public   void handleMessage(android.os.Message msg) {
  11. if (msg.what== 0x123 )
  12. {
  13. tv.setText( "Updated TextView" );
  14. }
  15. };
  16. };
  17. @Override  
  18. protected   void onCreate(Bundle savedInstanceState) {
  19. super .onCreate(savedInstanceState);
  20. setContentView(R.layout.activity_main);
  21. tv = (TextView) findViewById(R.id.tv);
  22. new MyThread().start();
  23. }
  24. class MyThread extends Thread
  25. {
  26. @Override  
  27. public   void run() {
  28. // Delay update for two seconds  
  29. try {
  30. Thread.sleep( 2000 );
  31. } catch (InterruptedException e) {
  32. // TODO Auto-generated catch block  
  33. e.printStackTrace();
  34. }
  35. handler.sendEmptyMessage( 0x123 );
  36. }
  37. }
  38. }

2. Use AsyncTask asynchronous task

  • Note: The operation of updating the UI can only be performed in the onPostExecute(String result) method.

  1. package com.example.runonuithreadtest;
  2. import android.app.Activity;
  3. import android.os.AsyncTask;
  4. import android.os.Bundle;
  5. import android.widget.TextView;
  6. public   class MainActivity extends Activity {
  7. private TextView tv;
  8. @Override  
  9. protected   void onCreate(Bundle savedInstanceState) {
  10. super .onCreate(savedInstanceState);
  11. setContentView(R.layout.activity_main);
  12. tv = (TextView) findViewById(R.id.tv);
  13. new Yibu().execute();
  14. }
  15. class Yibu extends AsyncTask<String, String, String>
  16. {
  17. @Override  
  18. protected String doInBackground(String... params) {
  19. try {
  20. Thread.sleep( 2000 );
  21. } catch (InterruptedException e) {
  22. // TODO Auto-generated catch block  
  23. e.printStackTrace();
  24. }
  25. return   null ;
  26. }
  27. @Override  
  28. protected   void onPostExecute(String result) {
  29. // TODO Auto-generated method stub  
  30. tv.setText( "Updated TextView" );
  31. }
  32. }
  33. }

3. Use runOnUiThread(action) method

  1. package com.example.runonuithreadtest;
  2. import android.app.Activity;
  3. import android.os.Bundle;
  4. import android.widget.TextView;
  5. public   class MainActivity extends Activity {
  6. private TextView tv;
  7. @Override  
  8. protected   void onCreate(Bundle savedInstanceState) {
  9. super .onCreate(savedInstanceState);
  10. setContentView(R.layout.activity_main);
  11. tv = (TextView) findViewById(R.id.tv);
  12. new MyThread().start();
  13. }
  14. class MyThread extends Thread
  15. {
  16. @Override  
  17. public   void run() {
  18. runOnUiThread( new Runnable() {
  19. @Override  
  20. public   void run() {
  21. // TODO Auto-generated method stub  
  22. try {
  23. // Delay update for two seconds  
  24. Thread.sleep( 2000 );
  25. } catch (InterruptedException e) {
  26. e.printStackTrace();
  27. }
  28. tv.setText( "Updated TextView" );
  29. }
  30. });
  31. }
  32. }
  33. }

4. Use Handler's post (Runnabel) method

  1. package com.example.runonuithreadtest;
  2. import android.app.Activity;
  3. import android.os.Bundle;
  4. import android.os.Handler;
  5. import android.widget.TextView;
  6. public   class MainActivity extends Activity {
  7. private TextView tv;
  8. @Override  
  9. protected   void onCreate(Bundle savedInstanceState) {
  10. super .onCreate(savedInstanceState);
  11. setContentView(R.layout.activity_main);
  12. tv = (TextView) findViewById(R.id.tv);
  13. Handler handler = new Handler();
  14. handler.post( new Runnable(){
  15. @Override  
  16. public   void run() {
  17. try {
  18. // Delay update for two seconds  
  19. Thread.sleep( 2000 );
  20. } catch (InterruptedException e) {
  21. e.printStackTrace();
  22. }
  23. tv.setText( "Updated TextView" );
  24. }
  25. });
  26. }
  27. }

<<:  Build the mainstream App framework in ten minutes

>>:  Share: HTML5 game development experience and development tools

Recommend

Some thoughts on Android APP performance optimization

When it comes to Android phones, most people have...

Tik Tok, Kuaishou or Bilibili? How do brands choose short videos?

Recently, Bilibili has become popular because of ...

Mango TV's president changed due to Hunan Radio and Television Group's listing

Zhang Ruobo, general manager of Happy Sunshine In...

This is the Nth pain point of iOS: I couldn’t agree more

Apple will hold its annual Worldwide Developers C...

3 tips for Tmall 618 marketing and attracting new customers!

The 618 Shopping Festival, the biggest e-commerce...

Hammer T2 detailed parameters announced: main camera parameters revealed

Smartisan Technology has announced that it will o...

How to become an excellent information flow advertising optimizer?

In fact, everyone has read a lot of useful inform...

This may be the most down-to-earth and clearest way to write an operation plan!

Friends who do operations will know that there wi...