Android CountDownTimer 예
이 Android 카운트다운 타이머 예제에서는 타이머 개체를 구현하여 ProgressBar에 진행률을 표시합니다. 이 자습서에서 빌드할 응용 프로그램은 사용자 경험을 향상시키기 위해 레벨을 완료하는 데 남은 시간이 그래픽으로 표시되는 Quiz 앱의 유용한 구성 요소입니다.
Android 카운트다운 타이머
Android CountDownTimer
클래스는 사용자가 정의한 미래의 시간까지 카운트다운을 예약하고 일정 간격으로 정기적인 알림을 보내는 데 사용됩니다. 이 클래스는 프로젝트에서 구현하기 위해 메서드를 재정의해야 하는 추상 클래스입니다. 클래스를 가져오려면 활동에 다음 줄을 추가해야 합니다. import android.os.CountDownTimer;
CountDownTimer 클래스의 관련 메서드는 다음과 같습니다.
synchronized final void cancel()
: 카운트다운을 취소할 때 사용합니다.abstract void onFinish()
: 타이머가 종료되면 이 콜백 메서드가 실행됩니다.abstract void onTick(long millisUntilFinished)
: 이 콜백 메서드는 일정한 간격으로 실행됩니다.synchronized final CountDownTimer start()
: 이 메서드는 카운트다운을 시작하는 데 사용됩니다.
CountDownTimer 클래스의 공용 생성자의 서명은 다음과 같습니다. CountDownTimer(long millisInFuture, long countDownInterval)
생성자의 매개변수는 다음과 같이 정의됩니다.
- millisInFuture : start() 호출부터 카운트다운이 완료되고 onFinish()가 호출될 때까지의 미래 밀리초 수
- countDownInterval: onTick(long) 콜백을 수신하기 위한 간격
이 프로젝트에서는 onTick() 메서드가 반복적으로 호출될 때 ProgressBar의 시간 값을 업데이트합니다.
Android 카운트다운 타이머 예제 프로젝트 구조
Android 카운트다운 타이머 코드
activity_main.xml은 두 개의 버튼, 즉 시작 및 중지 타이머 버튼과 시간을 표시하는 ProgressBar로 구성됩니다. activity_main.xml
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<ProgressBar
android:id="@+id/progressBar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:indeterminate="false"
android:max="10"
android:minHeight="50dp"
android:minWidth="200dp"
android:progress="0"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start Timer"
android:id="@+id/button"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="61dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Stop Timer"
android:id="@+id/button2"
android:layout_centerHorizontal="true"
android:layout_marginTop="46dp"
android:layout_below="@+id/progressBar" />
</RelativeLayout>
MainActivity.java는 다음과 같습니다.
package com.journaldev.countdowntimer;
import android.os.CountDownTimer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
ProgressBar progressBar;
Button start_timer,stop_timer;
MyCountDownTimer myCountDownTimer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
progressBar=(ProgressBar)findViewById(R.id.progressBar);
start_timer=(Button)findViewById(R.id.button);
stop_timer=(Button)findViewById(R.id.button2);
start_timer.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
myCountDownTimer = new MyCountDownTimer(10000, 1000);
myCountDownTimer.start();
}
});
stop_timer.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
myCountDownTimer.cancel();
}
});
}
public class MyCountDownTimer extends CountDownTimer {
public MyCountDownTimer(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
@Override
public void onTick(long millisUntilFinished) {
int progress = (int) (millisUntilFinished/1000);
progressBar.setProgress(progressBar.getMax()-progress);
}
@Override
public void onFinish() {
finish();
}
}
}
ProgressBar 프로젝트와 함께 Android CountDownTimer 다운로드
참조: 공식 문서