- 一、Handler和TimerTask相结合??
1、首先定义TimerTask
- TimerTask?task?=?new?TimerTask(){????
- ??public?void?run()?{????
- ??????Message?message?=?new?Message();????????
- ??????message.what?=?1;????????
- ??????handler.sendMessage(message);??????
- ??}????
- ??????
- };??
2、定义Handler
?
- Handler?handler?=?new?Handler()?{??
- ????public?void?handleMessage(Message?msg)?{??
- ????????switch?(msg.what)?{??
- ????????case?1:??
- ????????????break;??
- ????????}??
- ????????super.handleMessage(msg);??
- ????}??
- };??
3、用timer调用事件
- Timer?timer?=?new?Timer();???
- timer.schedule(task,?50);??
二、Android中自带的postDelayed
1、android view中的postDelayed
- v.postDelayed(new?Runnable()?{??
- ????????????????????public?void?run()?{??
- ?????
- ????????????????????}??
- ????????????????},?30);??
2、Handler中的postDelayed
- handler.postDelayed(new?Runnable()?{??
- ????????????????????public?void?run()?{??
- ?????
- ????????????????????}??
- ????????????????},?30); ?