1. 一个有趣Demo:
(1)定义一个handler变量
private final Handler mHandler = new Handler();
(2)定义一个Runnable变量
private Runnable onDoSomethingRunnable = new Runnable() {
public void run() {
onDoSomething();
}
};
(3)定义onDoSomething()方法:
public void onDoSomething(){
..................................
..................................
mHandler.postDelayed(onDoSomethingRunnable,150);
}
大家看一下上面的代码,发现了什么没。对,这个组合,可以 实现每隔一段时间,循环的执行某个操作。eg:每隔一秒,更新UI,每隔一段时间,去查看一个进程或者服务是否结束,............等等.当然了,大家可能会发现,一种方式可能有点消耗资源,因为他会循环的执行固定的操作,所以一般还是少用。
2. 一些关于Handler的事情:
A Handler allows you to send and process Message and Runnable objects associated with athread's MessageQueue
handler的作用,我就不说了,这种机制,我的理解是将一些非主UI线程更新UI,传递一些数据等等。当然,官方的说明:
There are two main uses for a Handler: (1) to schedule messages and runnablesto be executed as some point in the future; and (2) to enqueue anactionto be performed on a different thread than your own.
Handler中分发消息和runnable的一些方法:
post(Runnable),postAtTime(Runnable, long),postDelayed(Runnable, long),
sendEmptyMessage(int),sendMessage(Message),sendMessageAtTime(Message, long),sendMessageDelayed(Message, long)
Handler删除消息和runnable的方法:
removeCallbacks(Runnable), removeCallbacks(Runnable, Object ), removeCallbacksAndMessages(Object),
removeMessages(int), removeMessages(int , Object )
3.几个经典的DEMO。
(1)在一段数据处理的耗时操作后,如果这个操作超过1s,就显示进度条,如果耗时操作不超过1s,则不显示进度条后
//Handler处理消息
final Handler handler = new Handler();
final ProgressDialog progressDialog = new ProgressDialog(this);
progressDialog.setMessage(getText(R.string.adding_recipients));
progressDialog.setIndeterminate(true);
progressDialog.setCancelable(false);
final Runnable showProgress = new Runnable() {
public void run() {
progressDialog.show();
}
};
// Only show the progress dialog if we can not finish off parsing the return data in 1s,
// otherwise the dialog could flicker.
handler.postDelayed(showProgress, 1000);
new Thread(new Runnable() {
public void run() {
try {
//数据处理
} finally {
handler.removeCallbacks(showProgress);
progressDialog.dismiss();
}
}
}).start();
(2)更新UI界面
在需要更新页面的地方,发出更新界面的命令
private static final int UPDATE_PAGE_INDICATOR = 1;
/****更新用户选中的数目******/
private static final int UPDATE_SELE_COUNT = 2;
/****更新页面指示器******/
Message msg = myHandler.obtainMessage();
Bundle bundle = new Bundle();
//bundle.putInt("page", page);
bundle.putInt("page", PageCurrent);
msg.setData(bundle); // 向消息中添加数据
msg.what = UPDATE_PAGE_INDICATOR;
myHandler.sendMessage(msg); // 向Handler发送消息,更新UI
在需要更新指示选中数目的TextView的地方,发出更新界面的命令
myHandler.sendEmptyMessage(UPDATE_SELE_COUNT);
更新界面的操作,都放在myHandler 中
/***********myHandler:更新界面***********/
public Handler myHandler = new Handler(){
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch(msg.what)
{
case UPDATE_PAGE_INDICATOR :
Bundle data = msg.getData();
int page = (Integer) data.get("page");
/*****更新页面指示器******/
upDatePageIndicator(page);
break;
case UPDATE_SELE_COUNT:
mySelectCount.setText(""+selectCount);
break;
}
}
};
(3).Handler 和 Runnable 控制显示一个文本,1.5s后,让其不显示:
private TextView overlay = null;
LayoutInflater inflater = LayoutInflater.from(this);
overlay = (TextView) inflater.inflate(R.layout.overlay, null);
private Handler <strong>handler</strong>;
handler = new Handler();
private OverlayThread <strong>overlayThread</strong>;
overlayThread = new OverlayThread();
overlay.setText(sections[position]);
overlay.setVisibility(View.VISIBLE);
<strong>handler.removeCallbacks(overlayThread);
/*****延迟一秒后执行,让overlay为不可见*****/
handler.postDelayed(overlayThread, 1500);</strong>
/**
* 类描述:OverlayThread:实现设置overlay不可见
* @author hexiaoming
* @version
*/
private class OverlayThread implements Runnable {
/**
* 方法描述:run方法
* @param
* @return
* @see OverlayThread
*/
public void run() {
overlay.setVisibility(View.GONE);
}
}
参考资料:
1.Android的Handler总结
http://www.cnblogs.com/dawei/archive/2011/04/09/2010259.html