timer.cancel()之后TimerTask 和Timer 再次初始化就可以了
private Timer timer;
private TimerTask Runtask;
Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
// 要做的事情
if (m_Auto) {
task = new IDCardTask();
task.execute();
} else {
if (timer != null) {
timer.cancel();
timer = null;
bt_AutoReadidcard.setText("自动读卡");
}
}
super.handleMessage(msg);
}
};
bt_AutoReadidcard.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
if (m_Auto) {
m_Auto = false;
} else {
m_Auto = true;
timer = new Timer();
Runtask = new TimerTask() {
@Override
public void run() {
// TODO Auto-generated method stub
Message message = new Message();
message.what = 1;
handler.sendMessage(message);
}
};
timer.schedule(Runtask, 1000, 4000);
bt_AutoReadidcard.setText("停止读卡");
}
}
});
恩,做这个程序的目的是想 改变timer的触发间隔. 或者有什么其他方法可以改变的?
标签:Timer,cancel,new