상세 컨텐츠

본문 제목

Android - CountDownTimer 사용법

Android

by yjh0922 2022. 7. 12. 17:44

본문

# 변수 선언 부분
CountDownTimer timer;

//  타이머 전체 초 : 60초
    long millisInFuture = 60000; # final long millisInFuture 을 선언시 앱 실행 후 타이머를 변경할수 없다.
//  타이머 감소시킬 초 : 1초
    final int countDownInterval = 1000;
    
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        timer = new CountDownTimer(millisInFuture,countDownInterval)
            {
//          1초가 지날때 마다 어떤걸 할걸지
                @Override
                public void onTick(long l)
                {
                    int remain = (int) l / 1000;
        //              long remain = l / 1000;
		//              두개 다 사용 가능

                    txtTimer.setText("남은시간 : " + remain);
                }

    //          초가 다 지났을떄 어떤걸 할건지
                @Override
                public void onFinish()
                {
                    Log.i("MyTimer","타이머 끝났다");

                    isFinish = true;
                 }
              }
              
     }

 

 

사용방법이 나와있는 URL

https://developer.android.com/reference/android/os/CountDownTimer

 

CountDownTimer  |  Android Developers

android.net.wifi.hotspot2.omadm

developer.android.com

 

관련글 더보기