CountDownTimer 타이머 기능 구현
cancel() | 카운트다운을 취소 |
onFinish() | 시간이 다되면 콜백이 시작 |
onTick(long millisUnitlFinished) | 일정 간격으로 콜백이 실행 |
start() | 카운트다운을 시작 |
사용 예제
// 타이머를 사용하기 위한 멤버변수로 선언
CountDownTimer timer;
// 타이머 전체 초
final int millisInFuture = 10000;
// 감소시킬 초
final int countDownInterval = 1000;
@Override
protected void onCreate(Bundle savedInstanceState) {
...
// 10초 간 1초 간격으로 카운트다운을 진행한다.
timer = new CountDownTimer(millisInFuture, countDownInterval) {
@Override
public void onTick(long l) {
// 남은 시간을 계산해서 텍스트뷰에 보여준다.
long remain = l / 1000;
txtTimer.setText("남은 시간 : " + remain);
}
@Override
public void onFinish() {
Log.i("MyTimer :: ", "=================== 타이머 끝 ==================");
}
};
// 타이머 시작하기
timer.start();
}
'안드로이드 스튜디오' 카테고리의 다른 글
[ 안드로이드 ] 액티비티(Activity)와 인텐트(Intent), 단방향 / 양방향 데이터 전달 (0) | 2022.07.13 |
---|---|
[ 안드로이드 ] AlertDialog 사용법 (0) | 2022.07.12 |
[ 안드로이드 ] JSON 데이터 파싱하는 방법 (0) | 2022.07.12 |
[ 안드로이드 스튜디오 ] ScrollView(스크롤뷰) 사용법 (0) | 2022.07.12 |
[ 안드로이드 스튜디오 ] Activity LifeCycle 주요 4개 함수 (0) | 2022.07.11 |