*1 handler 基本概念
*2 handler 使用方法
*3 使用handler更新 ProgressBar
1、接受子线程发出的数据:通过Message 对象来传递数据
//创建Handler
Handler handler = new Handler() {
// 接受消息
public void handleMessage(Message msg) {
pb.setProgress(msg.arg1);
handler.post(runnable);
if (msg.arg1 == 80) {
handler.removeCallbacks(runnable);
}
};
2、实现对ProgressBar的操作
public class HandlerDemo2Activity extends Activity implements OnClickListener {
private Button start_btn, stop_btn;
private ProgressBar pb;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
start_btn = (Button) findViewById(R.id.start);
stop_btn = (Button) findViewById(R.id.stop);
pb = (ProgressBar) findViewById(R.id.pb);
start_btn.setOnClickListener(this);
stop_btn.setOnClickListener(this);
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.start:
handler.post(runnable);
break;
case R.id.stop:
handler.removeCallbacks(runnable);
break;
}
}