Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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
- Kotlin
- function
- Filter
- docker-compose
- 생명주기
- Interface
- docker
- Foreign Key
- react native
- AWS
- vuex
- elementAt
- mongoose
- Service
- recyclerview
- ReactNative
- Swift
- MINUS
- ConstraintLayout
- Generic
- union
- animation
- class component
- map
- LiveData
- collection
- CLASS
- list
- enum
- lifecycle
Archives
- Today
- Total
개발 일기
Broadcast Receiver ? 본문
android는 게시-구독 디자인 패턴과 유사한 브로드캐스트 메시지를 받거나 보낼수 있습니다.
ex) 통화 이벤트 , 시스템 부팅 , 기기 충전 , 비행기 모드 전환 , 비행기 모드 해제 등등
info)
- 명시적 브로드캐스트 (Explicit broadcast)
- 명시적으로 컴포넌트를 지정해준 경우
- 암시적 브로드캐스트 (Implicit broadcast)
- 호출에 대상이 명확하지 않은 경우
- API 26이상일 경우 manifest를 사용하여 암시적 브로드캐스트를 수신자를 선언할수 없습니다. (예외)
mainifest :)
<receiver android:name=".MyBroadcastReceiver" android:exported="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<action android:name="android.intent.action.INPUT_METHOD_CHANGED" />
</intent-filter>
</receiver>
private const val TAG = "MyBroadcastReceiver"
class MyBroadcastReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
StringBuilder().apply {
append("Action: ${intent.action}\n")
append("URI: ${intent.toUri(Intent.URI_INTENT_SCHEME)}\n")
toString().also { log ->
Log.d(TAG, log)
Toast.makeText(context, log, Toast.LENGTH_LONG).show()
}
}
}
}
Context :)
val br: BroadcastReceiver = MyBroadcastReceiver()
val filter = IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION).apply {
addAction(Intent.ACTION_AIRPLANE_MODE_CHANGED)
}
registerReceiver(br, filter)
보내는 방법 :)
// global 전송
sendBroadcast(Intent("com.example.NOTIFY"))
// global 전송 , 권한을 통해 특정 권한을 보유한 앱세트로 브로드 캐스트제한
sendBroadcast(Intent("com.example.NOTIFY"), Manifest.permission.SEND_SMS)
해당 앱에서만 전송이 가능한 방법 :)
// local broad cast 등록 (해당 앱의 정보를 밖으로 유출하지 않습니다.)
LocalBroadcastManager.getInstance(this).registerReceiver(mBroadcastReceiver,
IntentFilter("com.example.NOTIFY"));
// local broad cast 전송
LocalBroadcastManager.getInstance(context).sendBroadcast(Intent("com.example.NOTIFY"));
'Client > 안드로이드' 카테고리의 다른 글
ViewModel (0) | 2020.03.27 |
---|---|
Rxjava Subject (0) | 2020.03.26 |
Service (0) | 2020.03.26 |
paging library ? (페이징 라이브러리) (0) | 2020.03.24 |
Databinding ? (0) | 2020.03.21 |
Comments