应用场景:在指定的时间之后执行某操作
代码如下:TimerTestActivity.java
public class TimerTestActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Timer timer = new Timer(); timer.schedule(timerTask, 3000);// 在指定时间执行timerTask } TimerTask timerTask = new TimerTask() { @Override public void run() { Message message = new Message(); message.what = 1; handler.sendMessage(message);// 发送消息 } }; Handler handler = new Handler() { public void handleMessage(Message msg)// 处理消息 { switch (msg.what) { case 1: setTitle("Hello,I am Harvey"); Toast.makeText(TimerTestActivity.this, "Hello,I am Harvey", Toast.LENGTH_LONG).show(); break; default: break; } } }; }