일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
- sqlite
- room
- recyclerview
- 알림
- hilt
- notification
- kotlin
- Coroutine
- CoordinatorLayout
- Algorithm
- 백준
- 알고리즘
- 안드로이드
- DataBinding
- View
- lifecycle
- Behavior
- CollapsingToolbarLayout
- Navigation
- Android
- BOJ
- onLayout
- onMeasure
- HTTP
- ViewModel
- LiveData
- activity
- 코틀린
- AppBarLayout
- CustomView
- Today
- Total
개발일지
Android in A..Z - Broadcast Receiver 본문
Broadcast Receiver
Android에서 화면 켜짐, 핸드폰 켜짐, 문자 수신 등 시스템 이벤트가 발생했을 때 대처해야 하는 경우가 있다. Broadcast Receiver를 사용하여 이벤트를 수신할 수 있습니다.
Broadcast Receiver를 사용할 때 주의할 점은 리소스 관리입니다. Broadcast Receiver는 등록되면 계속해서 이벤트를 수신하고 동작하며 이는 메인 쓰레드에서 작업을 진행합니다(Handler를 따로 설정할 수 있지만 Default 값은 메인).
-> Broadcast Receiver에서 작업 처리가 오래 걸리는 경우 ANR이 발생할 수 있다.
* 시스템앱뿐만 아니라 다른 앱에서도 발생한 ACTION을 수신할 수 있습니다.
* Oreo 이후부터 백그라운드에 제한이 생기면서 BroadcastReciever에 제한이 생겼습니다.
https://developer.android.com/about/versions/oreo/background?hl=ko#broadcasts
백그라운드 실행 제한 | Android 개발자 | Android Developers
Android 8.0 이상을 대상으로 하는 앱에 대한 새로운 백그라운드 제한.
developer.android.com
정적 등록
BroadcastReciever를 정적으로 Manifest에 등록할 수 있습니다.
<receiver
android:name=".ScreenStateReceiver"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.SCREEN_ON" />
</intent-filter>
</receiver>
하지만 Oreo 이후 BroadcastReceiver에 제한이 생기면서 특정 Action만 정적 등록을 허용합니다. (허용되지 않은 Action은 수신할 수 없으며 동적으로 등록해야 합니다.)
https://developer.android.com/guide/components/broadcast-exceptions?hl=ko
암시적 브로드캐스트 예외 | Android 개발자 | Android Developers
백그라운드 제한에서 제외되는 암시적 브로드캐스트입니다.
developer.android.com
동적 등록
BroadcastReceiver를 Context를 통해 동적으로 등록합니다. 등록한 Context가 소멸되면 BroadcastReceiver도 사라집니다.
ex) Activity가 destroy되면 Activity로 등록한 BroadcastReceiver는 사라집니다.
class ScreenStateReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
when(intent.action) {
Intent.ACTION_SCREEN_ON -> {
onScreenOn(context)
}
}
}
private fun onScreenOn(context: Context) {
Toast.makeText(context, "Screen On", Toast.LENGTH_SHORT).show()
}
}
ScreenStateReceiver().also { receiver ->
screenStateReceiver = receiver
registerReceiver(receiver, IntentFilter(Intent.ACTION_SCREEN_ON))
}
Broadcasting
sendBroadcast
Intent를 통해 Broadcasting할 수 있다. Intent에 ACTION을 추가하여 Broadcasting할 수 있으며 putExtra를 통해 값을 전달할 수 있다.
권한으로 Broadcast 제한하기
BroadcastReceiver에 Permission을 추가하는 방법
정적 : Manifest에서 Receiver에게 Permission을 등록한다.
<receiver
android:name=".ScreenStateReceiver"
android:permission="com.taetae98.permission.SCREEN_STATE"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.SCREEN_ON" />
</intent-filter>
</receiver>
동적 : registerReceiver를 통해 등록할 수 있다.
registerReceiver(
receiver,
IntentFilter(Intent.ACTION_SCREEN_ON),
"com.taetae98.permission.SCREEN_STATE",
null
)
권한으로 제한된 발송
: 해당 권한을 가지고 있는 BroadcastReceiver만 수신할 수 있다.
sendBroadcast(intent, "com.taetae98.permission.SCREEN_STATE")
메시지 수신
BroadcastReceiver에서 onReceiver를 override하여 처리할 수 있다. 여기서 Intent를 통해 값을 전달 받을 수 있으며 Context를 통해 추가 작업을 진행할 수 있다.
class ScreenStateReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
when(intent.action) {
Intent.ACTION_SCREEN_ON -> {
onScreenOn(context)
}
}
}
private fun onScreenOn(context: Context) {
Toast.makeText(context, "Screen On", Toast.LENGTH_SHORT).show()
}
}
'Android (안드로이드)' 카테고리의 다른 글
Android in A..Z - Launch Mode (Manifest) (0) | 2021.10.12 |
---|---|
Android in A..Z - Manifest (0) | 2021.10.12 |
Android in A..Z - Service (0) | 2021.10.07 |
Android in A..Z - Fragment (0) | 2021.10.04 |
Android in A..Z - Glide Advanced (0) | 2021.10.01 |