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

Zhihu traffic mining methodology: How to mine most effectively?

Doing well on Zhihu is equivalent to doing well o...

iPhone 8's revolutionary front camera exposed: supports 3D perception

KGI Securities analyst Ming-Chi Kuo recently revea...

What is so lacking about domestic operating systems?

Recently, the release of Ubuntu Kylin 15.04, a do...

How much bandwidth is needed to rent a server for APP with 1 million users?

In the 5G smart era, I believe that everyone’s sm...

Huawei files lawsuit against FCC, alleging it violates U.S. Constitution

[[284776]] On December 5, Huawei's Shenzhen h...

Is the Canada goldenrod, which is "despised by everyone," a flower or a weed?

Produced by: Science Popularization China Produce...

The secret behind the popularity of live streaming marketing

To put it bluntly, live streaming is still a show...