Android异步更新UI的四种方式

移动开发 Android
大家都知道由于性能要求,android要求只能在UI线程中更新UI,要想在其他线程中更新UI,我大致总结了4种方式,欢迎补充纠正

大家都知道由于性能要求,android要求只能在UI线程中更新UI,要想在其他线程中更新UI,我大致总结了4种方式,欢迎补充纠正:

[[147881]]

  1. 使用Handler消息传递机制;

  2. 使用AsyncTask异步任务;

  3. 使用runOnUiThread(action)方法;

  4. 使用Handler的post(Runnabel r)方法;

下面分别使用四种方式来更新一个TextView。

1.使用Handler消息传递机制

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("更新后的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) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
   } 
   handler.sendEmptyMessage(0x123); 
  } 


  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.

2. 使用AsyncTask异步任务

  • 注:更新UI的操作只能在onPostExecute(String result)方法中。

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) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
   } 
   return null
  } 
  @Override 
  protected void onPostExecute(String result) { 
   // TODO Auto-generated method stub 
   tv.setText("更新后的TextView"); 
  } 


  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.

3. 使用runOnUiThread(action)方法

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() { 
     // TODO Auto-generated method stub 
      try { 
       //延迟两秒更新 
       Thread.sleep(2000); 
      } catch (InterruptedException e) { 
       e.printStackTrace(); 
      } 
      tv.setText("更新后的TextView"); 
    } 
   }); 
  } 


  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.

4. 使用Handler的post(Runnabel r)方

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("更新后的TextView"); 
   } 
  }); 


  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.

 

责任编辑:王雪燕 来源: segmentfault
相关推荐

2013-06-14 15:24:57

Android开发移动开发数据存储方式

2017-04-17 19:31:03

Android多线程

2014-12-25 09:41:15

Android加载方式

2020-06-12 08:28:29

JavaScript开发技术

2023-05-22 08:03:28

JavaScrip枚举定义

2022-03-25 14:47:24

Javascript数据类型开发

2010-07-28 13:54:42

Flex数据绑定

2013-10-17 09:25:52

2021-12-22 09:34:01

Golagn配置方式

2024-03-20 15:33:12

2011-05-20 09:55:26

Oracle连接

2022-10-27 14:18:13

Flowable流程变量

2025-01-20 15:50:19

2021-06-25 08:00:00

物联网医疗技术

2015-04-13 11:39:26

VDI灾难恢复

2015-04-02 16:54:52

灾难恢复VDI灾难恢复

2021-07-14 10:31:15

JavaScript开发 技巧

2021-12-01 15:40:40

节日开源剪贴画

2022-07-04 08:29:13

electron通信

2024-01-17 13:56:00

Redis节点映射关系
点赞
收藏

51CTO技术栈公众号