μλ νμΈμ π
μ€λμ μλλ‘μ΄λ λΈλ‘λμΊμ€νΈ 리μλ²μ
λνμ¬ κ³΅λΆν λ΄μ©μ ν¬μ€ν ν΄λ³΄λ € ν©λλ€.

BroadcastReceiver(λΈλ‘λμΊμ€νΈ 리μλ²)λ?
Android ꡬμ±μμ κ°μ λ©μμ§λ₯Ό μ λ¬ν λ μ¬λ¬ λμλ€μκ² νκΊΌλ²μ λ©μμ§λ₯Ό μ λ¬
ν νμκ° μμ λ μ¬μ©νλ μλλ‘μ΄λμ 4λ μ»΄ν¬λνΈ μ€ νλμ
λλ€.( ꡬμ±μμ κ° λ©μμ§λ Intentλ₯Ό ν΅ν΄ μ λ¬ν©λλ€.)
BroadcastReceiver λμ λ±λ‘κ³Ό μ μ λ±λ‘
μ μ 리μλ²λ μ±μ΄ μ’ λ£κ° λ νμλ λΈλ‘λμΊμ€νΈλ₯Ό λ°μΌλ©΄ ν΄λΉ Intentμ λνμ¬ μ¬μ μ μΈν νλ λͺ λ Ήμ μ€ννκ² λ©λλ€.
λ°λ©΄, λμ 리μλ²λ μ±μ΄ μ€νλκ³ μλ μ€μλ§ λ°©μ‘μ λ°μ λͺ λ Ήμ μ€νν μ μμ΅λλ€.
μ μ 리μλ² μμ± λ°©λ²
class SilenceBroadcastReceiver : BroadcastReceiver(){
override fun onReceive(p0: Context?, p1: Intent?) {
}
}
<receiver android:name=".SilenceBroadcastReceiver"
android:exported="true">
<!--Android 12 μ΄μ λΆν°λ exportedλ₯Ό μ§μ ν΄μ£Όμ΄μΌ ν©λλ€.-->
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
μ λ°©λ²μ λΈλ‘λμΊμ€νΈ 리μλ²λ₯Ό μ μ μΌλ‘ λ±λ‘νλ λ°©λ²μ λλ€.
1. μ μ½λμ κ°μ΄ λΈλ‘λμΊμ€νΈ 리μλ²λ₯Ό μμλ°μ ν΄λμ€λ₯Ό νλ μμ± ν
2. manifestνμΌμ receiverνκ·Έλ₯Ό ν΅ν΄ λΈλ‘λμΊμ€νΈ 리μλ²λ₯Ό λ±λ‘μμΌμ€λλ€.
3. λ§μ§λ§μΌλ‘ ν΄λΉ 리μλ²μ intent-filterνκ·Έλ₯Ό ν΅ν΄ μ λ¬λ°μ actionμ μ μν΄μ€ ν
λΈλ‘λμΊμ€νΈ 리μλ² ν΄λμ€μμ ν΄λΉ μ‘μ μμ μμ μ·¨ν νλμ μ μν΄μ£Όλ©΄ λ©λλ€.
λμ 리μλ² μμ± λ°©λ²
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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=".BroadCastActivity">
<TextView
android:id="@+id/brMessage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="50dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:textSize="15sp"
android:textColor="@android:color/holo_blue_dark"
android:text="Broadcast Message Display" />
<Button
android:id="@+id/sendBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Send BR"
android:textAllCaps="false"
android:textSize="20sp"
android:layout_centerInParent="true"
/>
</RelativeLayout>
λΈλ‘λ μΊμ€νΈ 리μλ²λ₯Ό μ μν MainActivity λ΄λΆ inner class
inner class MyBroadcastReceiver() : BroadcastReceiver() {
override fun onReceive(context: Context, receivedIntent: Intent?) {
val intentValue = receivedIntent?.getIntExtra("extraKey" , -1)
if(receivedIntent?.action == BR_ACTION_NAME){
Log.d(TAG, "onReceive: Action Received!!!")
binding.brMessage.text = String.format("λΈλ‘λ μΊμ€νΈ %dλ² μ€νλ¨" , intentValue)
}
}
}
}
MainActivity.kt
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import com.lee.mainbroadcastexample.databinding.ActivityBroadcastBinding
const val BR_ACTION_NAME = "com.lee.br.send.BR_MY_ACTION"
class BroadCastActivity : AppCompatActivity() {
private val TAG = "BroadCastActivity"
private lateinit var binding : ActivityBroadcastBinding
private var mBroadCastReceiver : BroadcastReceiver? = null
private var increment = 0
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityBroadcastBinding.inflate(layoutInflater).also {
setContentView(it.root)
}
binding.sendBtn.setOnClickListener {
with(Intent(BR_ACTION_NAME)){
putExtra("extraKey" , ++increment)
sendBroadcast(this)
Log.d(TAG, "sendIntent!!")
}
}
}
override fun onStart() {
super.onStart()
val intentFilter = IntentFilter()
intentFilter.addAction(BR_ACTION_NAME)
mBroadCastReceiver = MyBroadcastReceiver()
registerReceiver(mBroadCastReceiver , intentFilter)
}
override fun onStop() {
super.onStop()
unregisterReceiver(mBroadCastReceiver)
}
inner class MyBroadcastReceiver() : BroadcastReceiver() {
override fun onReceive(context: Context, receivedIntent: Intent?) {
val intentValue = receivedIntent?.getIntExtra("extraKey" , -1)
if(receivedIntent?.action == BR_ACTION_NAME){
Log.d(TAG, "onReceive: Action Received!!!")
binding.brMessage.text = String.format("λΈλ‘λ μΊμ€νΈ %dλ² μ€νλ¨" , intentValue)
}
}
}
}
λμ 리μλ²λ μμ κ°μ΄ μ‘ν°λΉν° λ΄λΆμμ λΈλ‘λμΊμ€νΈ 리μλ²λ₯Ό μ μν΄μ€λλ€.
onStart()μμ IntentFilterμ λΈλ‘λμΊμ€νΈλ¦¬μλ²λ₯Ό μμ±ν΄μ£Όκ³
registerReceiver()λ₯Ό ν΅ν΄ λΈλ‘λ μΊμ€νΈ 리μλ²λ₯Ό λμ μΌλ‘ λ±λ‘ν΄μ€λλ€.
μ΄ν λμμ μ μ 리μλ²μ λμΌν©λλ€.
λ€λ§, λμ 리μλ²μ κ²½μ° unregisterReceiver()λ₯Ό ν΅ν΄ λμ μΌλ‘ 리μλ²λ₯Ό ν΄μ ν΄μ€ μλ μμ΅λλ€. π
μ μ νκ² ν΄μ νμ¬ λ©λͺ¨λ¦¬ λ¦μ΄ λ°μνμ§ μλλ‘ ν©μλ€!!!! π€
μ΄μμΌλ‘ λΈλ‘λ μΊμ€νΈ 리μλ²μ κ΄ν ν¬μ€ν μ λ§μΉκ² μ΅λλ€.
μ€λλ μ¦μ½νμΈμ :)