반응형
안드로이드 개발중 타이머(Timer)를 사용하는 방법을 알아보도록 합니다.
사용하는 클래스
Timer
TimerTask
지정된 지연후 실행
public void schedule(TimerTask task, long delay);
task : 예약할 작업
delay : 작업이 실행되기 전 지연 시간(ms)
지정된 지연후 반복실행
public void schedule(TimerTask task, long delay, long period);
task : 예약할 작업
delay : 작업이 실행되기 전 지연 시간(ms)
period : 연속 작업 실행 사이의 시간(ms)
타이머 종료
timer.cancel();
예제)
private Timer timer;
public int count;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
timer = new Timer();
TimerTask timerTask = new TimerTask() {
@Override
public void run() {
TextView tv = findViewById(R.id.textView2);
tv.setText("count:"+count++);
}
};
count = 0;
// timer 실행
timer.schedule(timerTask, 10000, 10000); // 10초후 10초간격
}
// timer 종료
@Override
protected void onDestroy() {
timer.cancel();
super.onDestroy();
}
반응형
'개발' 카테고리의 다른 글
[Python] 주피터 노트북(Jupyter Notebook)이 뭐지? 아나콘다(Anaconda)는 또 뭐야? (feat. 파이썬 R프로그래밍) (4) | 2023.01.10 |
---|---|
[Java/Android] Calendar를 이용해 날짜, 시간 가져오는 방법 (0) | 2022.11.07 |
[Android] 안드로이드 스튜디오 logcat 특정문구 제외 방법, 색깔지정 방법 (0) | 2022.10.20 |
댓글