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

Smart hardware is developing too fast, please wait for consumers!

2014 is coming to an end, and it is clear that in...

At the critical moment, it can save your life!

Source: Dr. Curious (haoqi238) The cover image of...

Why can't humans drink raw water, but animals drink it directly?

In nature documentaries, we often see this scene:...

How to Use Push Notifications in iOS 10

Introduction Although often overused, push notifi...

To deal with Omicron, don’t choose the wrong mask!

The epidemic situation caused by the Omicron vari...

China's greatest connection! How many secrets does the Yangtze River hide?

On Earth There has never been such a river With s...

Counting: Ancient Chinese Mathematical Wisdom on Small Sticks

The numbers that seem simple to modern people are...