Android Application

Kotlin 브로드캐스트 리시버 : 시간 변화에 따른 Listener 이벤트를 발생

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

첨부파일

Kotlin 브로드캐스트 리시버(BroadcastReceiver,BR) : 시간 변화에 따른 Listener 이벤트를 발생
 ACTION_TIME_TICK은 시간의 변화를 감지하는 Action 이다.
앱 안에서 Scope나 Async를 통해 delay()를 주지 않고, 
1분 단위로 브로드캐스트 메시지를 수신하도록 하는 Action 이다.

-------------------------------------------------------------------------------------
      activity_main.xml
-------------------------------------------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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">

    <TextView
        android:id="@+id/TimeTickTV"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=" 분이 바뀌면\n 시간 브로드캐스트가 발생"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

------------------------------------------------------------------------------------
    MainActivity.kt
------------------------------------------------------------------------------------
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        setBroadcastReceiver()
    }

    private fun setBroadcastReceiver(){
        val intent = IntentFilter(Intent.ACTION_TIME_TICK)
        val receiver = TimeCheckBroadcastReceiver()
        registerReceiver(receiver,intent)
    }
}

------------------------------------------------------------------------------------
      TimeCheckBroadcastReceiver.kt
-- New --> Other  --> Broadcast Receiver
-- Class Name : TimeCheckBroadcastReceiver
------------------------------------------------------------------------------------
class TimeCheckBroadcastReceiver : BroadcastReceiver() {

    override fun onReceive(context: Context, intent: Intent) {
        Toast.makeText(context,"Time Check",Toast.LENGTH_SHORT).show()
    }
}

------------------------------------------------------------------------------------
    themes.xml
------------------------------------------------------------------------------------
<resources xmlns:tools="http://schemas.android.com/tools">
    <!-- Base application theme. -->
    <style name="Base.Theme.BroadcastReceiveTimeCheck" parent="Theme.AppCompat.Light">
        <item name="colorPrimary">#FF9800</item>
        <item name="colorPrimaryDark">#00897B</item>
    </style>
    <style name="Theme.BroadcastReceiveTimeCheck" parent="Base.Theme.BroadcastReceiveTimeCheck" />
</resources>