프론트엔드/안드로이드 스튜디오
안드로이드 스튜디오 : 핸들러를 이용해서 실시간 현재 시간 가져오기
하늘.98
2022. 3. 22. 18:06
public class MainActivity extends Activity {
private TextView timeTv;
private Timer mTimer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
timeTv = (TextView) findViewById(R.id.timeTv);
MainTimerTask timerTask = new MainTimerTask();
mTimer = new Timer();
mTimer.schedule(timerTask, 500, 1000);
}
private Handler mHandler = new Handler();
private Runnable mUpdateTimeTask = new Runnable() {
public void run() {
Date rightNow = new Date();
SimpleDateFormat formatter = new SimpleDateFormat(
"yyyy.MM.dd hh:mm:ss ");
String dateString = formatter.format(rightNow);
timeTv.setText(dateString);
}
};
class MainTimerTask extends TimerTask {
public void run() {
mHandler.post(mUpdateTimeTask);
}
}
@Override
protected void onDestroy() {
mTimer.cancel();
super.onDestroy();
}
@Override
protected void onPause() {
mTimer.cancel();
super.onPause();
}
@Override
protected void onResume() {
MainTimerTask timerTask = new MainTimerTask();
mTimer.schedule(timerTask, 500, 3000);
super.onResume();
}
}
출처: https://pikac.tistory.com/190 [pikac]
안드로이드 현재시간 TextView 출력
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 public class MainActivity e..
pikac.tistory.com