Notice
Recent Posts
Recent Comments
Link
«   2024/05   »
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
Tags more
Archives
Today
Total
관리 메뉴

맨땅에 코딩

[Android] Timer 구현 - Handler 본문

앱 개발/Java

[Android] Timer 구현 - Handler

맨땅 2022. 9. 13. 15:42

목차

    반응형

     

     

     

    - Timer 시작 버튼을 누르면 카운팅

    - 일시정지 버튼을 통해서 타이머 일시정지

    - 시작 버튼을 다시 누르면 멈췄던 숫자에서 재 카운팅

     

     


     

    Layout 디자인

     

     

    Log 기록

     

     

    [MainActivity.java]

    public class MainActivity extends AppCompatActivity {
    
        private static final int MESSAGE_TIMER1_START = 100;
        
        TimerHandler timerHandler = null;
    
        Button buttonStart, buttonStop;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            timerHandler = new TimerHandler();
    
            buttonStart = findViewById(R.id.buttonStart);
            buttonStop = findViewById(R.id.buttonStop);
    
            buttonStart.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    timerHandler.sendEmptyMessage(MESSAGE_TIMER1_START);
                }
            });
            buttonStop.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    timerHandler.removeMessages(MESSAGE_TIMER1_START);
                }
            });
        }
    
        private class TimerHandler extends Handler {
            int count = 0;
    
            @Override
            public void handleMessage(Message msg) {
                switch(msg.what) {
                    case MESSAGE_TIMER1_START:
                        Log.d("TimerHandler", "Timer Start : " + count++);
                        this.sendEmptyMessageDelayed(MESSAGE_TIMER1_START, 1000);
                        break;
                }
            }
        }
    }

     

     

    [activity_main.xml]

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">
    
        <Button
            android:id="@+id/buttonStart"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="타이머 시작"
            app:layout_constraintBottom_toTopOf="@+id/buttonStop"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.498"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.866" />
    
        <Button
            android:id="@+id/buttonStop"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="312dp"
            android:text="타이머 멈춤(일시정지)"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.498"
            app:layout_constraintStart_toStartOf="parent" />
    </android.support.constraint.ConstraintLayout>

     

     

     

     

    반응형