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

User Operation: How to correctly design a paid membership system?

In the past month, I have had some exchanges and ...

Luckin Coffee’s private domain operation strategy!

From setting the world's fastest IPO record t...

Oxylabs experts discuss whether reinforcement (machine) learning is overhyped?

Suppose you sit down to play chess with a friend,...

SAIC Motor delivered 750 “government reception vehicles” to the CIIE

(October 12, 2024, Shanghai) Today, the "Del...

Five elements of user reach: materials, channels, and objects!

My definition of user reach is: sending specific ...

The core model and skills of community operation!

This year, the term " community operation &q...

Methodology: Brand promotion from 0-1!

Last year, I thought that after working in brand ...

How far can a taxi-hailing app that has forgotten its original purpose go?

Frankly speaking, I am a loyal user of taxi-haili...