Handler
private Handler handler = new Handler();
Runnable
private Runnable runnable = new Runnable() {
@Override
public void run() {
this.upDate();
handler.postDelayed(this, 1000);
}
void upDate() {
setTime();
}
};
设置时间
private void setTime() {
//日期,api 24暂定
SimpleDateFormat formatter = new SimpleDateFormat("yyyy年 MM月 dd日 HH : mm : ss", Locale.getDefault());
String show_timeStr = formatter.format(new Date(System.currentTimeMillis()));
tv_clock1.setText(show_timeStr);
}
启动计时(建议在onCreate()中)
handler.postDelayed(runnable, 1000);
释放线程(建议在onDestroy()中)
handler.removeCallbacks(runnable);