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]] Use the Handler message passing mechanism; Use AsyncTask asynchronous task; Use runOnUiThread(action) method; Use the post(Runnabel r) method of Handler;
The following four methods are used to update a TextView. 1. Use Handler message passing mechanism - package com.example.runonuithreadtest;
- import android.app.Activity;
- import android.os.Bundle;
- import android.os.Handler;
- import android.widget.TextView;
- public class MainActivity extends Activity {
- private TextView tv;
- Handler handler = new Handler()
- {
- public void handleMessage(android.os.Message msg) {
- if (msg.what== 0x123 )
- {
- tv.setText( "Updated TextView" );
- }
- };
- };
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super .onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- tv = (TextView) findViewById(R.id.tv);
- new MyThread().start();
- }
- class MyThread extends Thread
- {
- @Override
- public void run() {
-
- try {
- Thread.sleep( 2000 );
- } catch (InterruptedException e) {
-
- e.printStackTrace();
- }
- handler.sendEmptyMessage( 0x123 );
- }
- }
- }
2. Use AsyncTask asynchronous task - package com.example.runonuithreadtest;
- import android.app.Activity;
- import android.os.AsyncTask;
- import android.os.Bundle;
- import android.widget.TextView;
- public class MainActivity extends Activity {
- private TextView tv;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super .onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- tv = (TextView) findViewById(R.id.tv);
- new Yibu().execute();
- }
- class Yibu extends AsyncTask<String, String, String>
- {
- @Override
- protected String doInBackground(String... params) {
- try {
- Thread.sleep( 2000 );
- } catch (InterruptedException e) {
-
- e.printStackTrace();
- }
- return null ;
- }
- @Override
- protected void onPostExecute(String result) {
-
- tv.setText( "Updated TextView" );
- }
- }
- }
3. Use runOnUiThread(action) method - package com.example.runonuithreadtest;
- import android.app.Activity;
- import android.os.Bundle;
- import android.widget.TextView;
- public class MainActivity extends Activity {
- private TextView tv;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super .onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- tv = (TextView) findViewById(R.id.tv);
- new MyThread().start();
- }
- class MyThread extends Thread
- {
- @Override
- public void run() {
- runOnUiThread( new Runnable() {
- @Override
- public void run() {
-
- try {
-
- Thread.sleep( 2000 );
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- tv.setText( "Updated TextView" );
- }
- });
- }
- }
- }
4. Use Handler's post (Runnabel) method - package com.example.runonuithreadtest;
- import android.app.Activity;
- import android.os.Bundle;
- import android.os.Handler;
- import android.widget.TextView;
- public class MainActivity extends Activity {
- private TextView tv;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super .onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- tv = (TextView) findViewById(R.id.tv);
- Handler handler = new Handler();
- handler.post( new Runnable(){
- @Override
- public void run() {
- try {
-
- Thread.sleep( 2000 );
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- tv.setText( "Updated TextView" );
- }
- });
- }
- }
|