http://www.javaeye.com/topic/422972
写惯java的人可能会认为Timer的使用应该如下:
程序功能很简单:10秒后,SetTitle为“hear me?”,然后,实际上这样做是不行的,这跟Android的线程安全有关!
- package com.ray.test;
- import java.util.Timer;
- import java.util.TimerTask;
- import android.app.Activity;
- import android.os.Bundle;
- public class JavaTimer extends Activity {
- Timer timer = new Timer();
- TimerTask task = new TimerTask(){
- public void run() {
- setTitle("hear me?");
- }
- };
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- timer.schedule(task, 10000);
- }
- }