android 子线程更新UI

时间:2024-01-10 15:33:26
参考http://examples.javacodegeeks.com/android/core/os/handler/android-handler-example/
package com.example.test1;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.widget.Button;
import android.widget.ProgressBar;
/*
* 使用Handler更新UI
* */
public class MyHandlerActivity2 extends Activity {
Button button;
ProgressBar bar;
Handler myHandler;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.startButton);
bar=(ProgressBar) findViewById(R.id.bar);
new Thread(MyThread).start();
// 当创建一个新的Handler实例时, 它会绑定到当前线程和消息的队列中,开始分发数据
// Handler有两个作用, (1) : 定时执行Message和Runnalbe 对象
// (2): 让一个动作,在不同的线程中执行.
// 它安排消息,用以下方法
// post(Runnable)
// postAtTime(Runnable,long)
// postDelayed(Runnable,long)
// sendEmptyMessage(int)
// sendMessage(Message);
// sendMessageAtTime(Message,long)
// sendMessageDelayed(Message,long)
// 以上方法以 post开头的允许你处理Runnable对象
//sendMessage()允许你处理Message对象(Message里可以包含数据,)
myHandler = new Handler(){
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
Log.d("MyHandler", "handleMessage......");
//super.handleMessage(msg);
// 此处可以更新UI
Bundle b = msg.getData();
int value = b.getInt("value")*10;
MyHandlerActivity2.this.bar.setProgress(value);
}
};
//myHandler.postDelayed(MyThread, 1500);
//myHandler.post(MyThread);//并没有启动新线程
}
Runnable MyThread =new Runnable() {
public void run() {
for (int i = 0; i <= 10; i++) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Log.d("thread.......", "mThread........");
Message msg = new Message();
Bundle b = new Bundle();// 存放数据
b.putInt("value", i);
msg.setData(b);
//myHandler.sendMessage(myHandler.obtainMessage());//发送的都是0
myHandler.sendMessage(msg); // 向Handler发送消息,更新UI
}
//myHandler.removeCallbacks(MyThread);
}
};
}

方法2

package com.example.test1;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.widget.Button;
import android.widget.ProgressBar;
/*
* 使用Handler更新UI
* */
public class MyHandlerActivity2 extends Activity {
Button button;
ProgressBar bar;
Handler myHandler;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.startButton);
bar=(ProgressBar) findViewById(R.id.bar);
new Thread(MyThread).start();
// 当创建一个新的Handler实例时, 它会绑定到当前线程和消息的队列中,开始分发数据
// Handler有两个作用, (1) : 定时执行Message和Runnalbe 对象
// (2): 让一个动作,在不同的线程中执行.
// 它安排消息,用以下方法
// post(Runnable)
// postAtTime(Runnable,long)
// postDelayed(Runnable,long)
// sendEmptyMessage(int)
// sendMessage(Message);
// sendMessageAtTime(Message,long)
// sendMessageDelayed(Message,long)
// 以上方法以 post开头的允许你处理Runnable对象
//sendMessage()允许你处理Message对象(Message里可以包含数据,)
myHandler = new Handler(){
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
Log.d("MyHandler", "handleMessage......");
//super.handleMessage(msg);
// 此处可以更新UI
Bundle b = msg.getData();
int value = b.getInt("value")*10;
MyHandlerActivity2.this.bar.setProgress(value);
}
};
//myHandler.postDelayed(MyThread, 1500);
//myHandler.post(MyThread);//并没有启动新线程
}
Runnable MyThread =new Runnable() {
public void run() {
for (int i = 0; i <= 10; i++) {
final int value=i;
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
myHandler.post(new Runnable() { @Override
public void run() {
//Cannot refer to a non-final variable value inside an inner class defined in a different method
//因此value要设置为final
bar.setProgress(value*10);
}
});
}
}
};
}