Android Application

Kotlin 스레드(Thread) : AlarmThread와 Toast

작성자 임베디드코리아 작성일23-10-23 23:27 조회1,479회 댓글0건

첨부파일

Kotlin 멀티미디어 : AlarmThread와 Toast
- Thread에서 Alarm을 이용하여 5초에 한 번씩 나타남

-------------------------------------------------------------------------------------
      activity_main.xml
-------------------------------------------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/StartButton"
        android:text="쓰레드 시작"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/confirm_botton"
        android:layout_weight="1"
        android:layout_marginTop="30dp"
        android:layout_marginLeft="20dp" />
    <Button
        android:id="@+id/StopButton"
        android:text="쓰레드 종료"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/confirm_botton"
        android:layout_weight="1"
        android:layout_marginTop="30dp"
        android:layout_marginLeft="10dp" />

    <TextView
        android:id="@+id/statusTV"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="상태 메시지"
        android:layout_weight="1"
        android:layout_marginTop="30dp"
        android:layout_marginLeft="10dp"/>
</LinearLayout>

------------------------------------------------------------------------------------
    botton_true.xml
-- res/drawable
-- New --> Drawable Resource File
-- File Name : button_true  , Root Element : shape
------------------------------------------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:padding="10dp"
    android:shape="rectangle" >
    <solid android:color="#8EC7F4" />
    <stroke android:color="#ff0000ff" android:width="4dp" />
    <padding android:left="7dp" android:top="7dp"
        android:right="7dp" android:bottom="7dp" />
    <corners
        android:radius="30dp"
        android:bottomLeftRadius="30dp"
        android:bottomRightRadius="30dp"
        android:topLeftRadius="30dp"
        android:topRightRadius="30dp" />
</shape>

------------------------------------------------------------------------------------
    botton_false.xml
-- res/drawable
-- New --> Drawable Resource File
-- File Name : button_farse  , Root Element : shape
------------------------------------------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:padding="10dp"
    android:shape="rectangle" >
    <solid android:color="#FFC107" />
    <stroke android:color="#ff0000ff" android:width="4dp" />
    <padding android:left="7dp" android:top="7dp"
        android:right="7dp" android:bottom="7dp" />
    <corners
        android:radius="30dp"
        android:bottomLeftRadius="30dp"
        android:bottomRightRadius="30dp"
        android:topLeftRadius="30dp"
        android:topRightRadius="30dp" />
</shape>

------------------------------------------------------------------------------------
    confirm_botton.xml
-- res/drawable
-- New --> Drawable Resource File
-- File Name : confirm_botton  , Root Element : selector
------------------------------------------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:drawable="@drawable/button_false"></item>
    <item android:state_pressed="false" android:drawable="@drawable/button_true"></item>
</selector>

------------------------------------------------------------------------------------
    MainActivity.kt
------------------------------------------------------------------------------------
class MainActivity : AppCompatActivity() {
    lateinit var tvSec: TextView

    // Thread 핸들러 생성
    private lateinit var handlerThread: HandlerThread
    private var toastAlarmThread:ToastAlarmThread? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        var StartBTN: Button= findViewById<Button>(R.id.StartButton)
        var StopBTN: Button= findViewById<Button>(R.id.StopButton)
        tvSec = findViewById(R.id.statusTV)

        handlerThread = HandlerThread("toastAlarmThread")
        handlerThread.start()

        tvSec.text = "준비중"

        StartBTN.setOnClickListener {
            toastAlarmThread = ToastAlarmThread()
            toastAlarmThread?.start()
        }
        StopBTN.setOnClickListener {
            toastAlarmThread?.stopThread()
        }
    }

    inner class ToastAlarmThread:Thread(){

        private var time = 0

        override fun run() {
            while(handlerThread.isAlive){
                Handler(handlerThread.looper).post{
                    Toast.makeText(this@MainActivity, "시작 Tread : $time 초 ${handlerThread.name}", Toast.LENGTH_SHORT).show()
                    tvSec.text = "$time 초"
                    time += 5
                }
                sleep(5000)
            }
        }
        fun stopThread(){
            Handler(handlerThread.looper).post{
                sleep(1000)
                tvSec.text = "준비중"
                Toast.makeText(this@MainActivity, "종료 Tread : ${handlerThread.name}", Toast.LENGTH_SHORT).show()

                handlerThread.quitSafely()
            }
            toastAlarmThread = null
        }
    }
}

------------------------------------------------------------------------------------
    themes.xml
------------------------------------------------------------------------------------
<resources xmlns:tools="http://schemas.android.com/tools">
    <!-- Base application theme. -->
    <style name="Base.Theme.Threadhandler" parent="Theme.AppCompat.Light">
        <item name="colorPrimary">#00BCD4</item>
        <item name="colorPrimaryDark">#FFC107</item>
    </style>

    <style name="Theme.Threadhandler" parent="Base.Theme.Threadhandler" />
</resources>