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

Everything about Bitcoin in 2014 is in this report

Overall, 2014 can be described as a “Tale of Two ...

What does Jack Ma’s “ecosystem” really mean?

Alibaba's listing has become the hottest head...

Do potatoes have “natural enemies”?

Potato (Latin name: Solanum tuberosum, English: P...

What should I do if oCPC matching words are very messy? | SEM Q&A

There are many difficult problems in bidding and ...

Real Physics Terms: From the Big Bang to Black Holes

In scientific research, every time scientists dis...

Ankang SEO Training: How to optimize the entire site to achieve good results?

How to better optimize the entire site is a quest...

How to name a brand? There are several common methods

The name should tell potential customers what the...

Which three “mobile industry chains” has WeChat created?

[[119993]] Editor's Tips: One of the criteria...

Teach you to thoroughly understand the KMP algorithm from beginning to end

[[121931]] References for this article: Data Stru...

Why online video can’t defeat cable TV in the end?

January 27 (Reporter Zhang Zhichang) Whether in t...