Android异步更新UI的四种方式

来源:互联网 发布:早岁那知世事艰 编辑:程序博客网 时间:2024/05/22 14:24

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

  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");}};};@Overrideprotected 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{@Overridepublic void run() {//延迟两秒更新try {Thread.sleep(2000);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}handler.sendEmptyMessage(0x123);}}}

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;@Overrideprotected 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>{@Overrideprotected String doInBackground(String... params) {try {Thread.sleep(2000);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}return null;}@Overrideprotected void onPostExecute(String result) {// TODO Auto-generated method stubtv.setText("更新后的TextView");}}}

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;@Overrideprotected 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{@Overridepublic void run() {runOnUiThread(new Runnable() {@Overridepublic void run() {// TODO Auto-generated method stubtry {//延迟两秒更新Thread.sleep(2000);} catch (InterruptedException e) {e.printStackTrace();}tv.setText("更新后的TextView");}});}}}

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;@Overrideprotected 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(){@Overridepublic void run() {try {//延迟两秒更新Thread.sleep(2000);} catch (InterruptedException e) {e.printStackTrace();}tv.setText("更新后的TextView");}});}}
0 0
原创粉丝点击