定时器的三种方法。
(1)Handler
//开始定时
public void startTimer() {
//移除MSG_TIMING
mHandler.removeMessages(MSG_TIMING);
Message msg = Message.obtain();
msg.what = MSG_TIMING;
msg.arg1 = TYPE_START;
//开始计时
mHandler.sendMessage(msg);
}
//停止计时
public void stopTimer() {
//移除MSG_TIMING
mHandler.removeMessages(MSG_TIMING);
}
public static final int MSG_TIMING = 1;
public static final int TYPE_START = 1;
public static final int TYPE_STOP = 2;
private Handler mHandler = new Handler() {
private int count = 0;
@Override
public void handleMessage(Message msg) {
int what = msg.what;
int type = msg.arg1;
if (what == MSG_TIMING) {
if (type == TYPE_START) {
count = 0;
}
// UI
// UI
//延迟1000ms
mHandler.sendEmptyMessageDelayed(MSG_TIMING, 1000);
count++;
}
}
};
ÿ