public class Course extends Activity {
private static boolean always = true;
private Timer timer = new Timer();
private TextView timeTextView;
private Handler mHandler;
private Thread mThread;
private Date currentTime;
private Runnable refreshRunnable = new Runnable() {
@Override
public void run() {
currentTime = new Date(System.currentTimeMillis());
String formatCurrentDate = sdf.format(currentTime);
timeTextView.setText(formatCurrentDate);
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// 显示时间
timeTextView = (TextView) findViewById(R.id.showTime);
sdf = new SimpleDateFormat("yyyy年MM月dd日 aaa HH:mm:ss E");
mHandler = new Handler();
mThread = new Thread() {
public void run() {
while (always == true) {
mHandler.post(refreshRunnable);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
};
mThread.start();
}
}