39

How to use register and create a Broadcast Receiver in Android in Kotlin. Any advice... In Java, you can create it by declaring it as a Broadcast Receiver. But in Kotlin I am not able to find Broadcast Receiver ...well if it is there then how to use it.

  • 1
    What do you mean saying 'there is no Broadcast Receiver function'? From Kotlin you can see the same classes which you can see from Java. – Miha_x64 Jul 30 '17 at 10:39

3 Answers3

98

you can do it in the following way

Create a broadcast receiver object in your activity class

val broadCastReceiver = object : BroadcastReceiver() {
    override fun onReceive(contxt: Context?, intent: Intent?) {   
        when (intent?.action) {
           BROADCAST_DEFAULT_ALBUM_CHANGED -> handleAlbumChanged()
           BROADCAST_CHANGE_TYPE_CHANGED -> handleChangeTypeChanged()
        }
    }
}

Register broadcast receiver in onCreate() function of your activity

 LocalBroadcastManager.getInstance(this)
                    .registerReceiver(broadCastReceiver, IntentFilter(BROADCAST_DEFAULT_ALBUM_CHANGED))

unregister it in ondestroy function of your activity

LocalBroadcastManager.getInstance(this)
                .unregisterReceiver(broadCastReceiver)
Kenneth
  • 30
  • 1
  • 4
v4_adi
  • 1,503
  • 1
  • 12
  • 14
  • 5
    @AcademyofProgrammer you need to register receiver in manifest if it is global broadcast. You don't to do it for local broadcast. – v4_adi Jan 31 '18 at 18:27
  • 1
    this could create a memory leak if you Android does not call `onDestroy()` which according to the docs is not guaranteed. onPause / onResume are a better pair – Thalatta Jun 12 '18 at 22:47
  • I agree with @Thalatta, register for your receiver at onResume() and unregister at onPause(), thats the recommended way. – iCantC Apr 25 '19 at 13:25
  • Wouldn't that stop it from receiving broadcasts in multi window inactive state? Isn't start/stop better? – behelit Jul 13 '20 at 00:06
11

Anonymous class syntax in Kotlin is like this:

val receiver = object : BroadcastReceiver() {
    override fun onReceive(context: Context, intent: Intent) {

    }
}
Miha_x64
  • 5,973
  • 1
  • 41
  • 63
alireza
  • 514
  • 3
  • 14
11

I've created a BroadcastReceiver Kotlin extension, which you can copy/paste anywhere. It doesn't do much more than what is already mentioned, but it reduces some of the boilerplate.

Using this extension, you should register/unregister like so:

private lateinit var myReceiver: BroadcastReceiver

override fun onStart() {
    super.onStart()
    myReceiver = registerReceiver(IntentFilter(BROADCAST_SOMETHING_HAPPENED)) { intent ->
        when (intent?.action) {
            BROADCAST_SOMETHING_HAPPENED -> handleSomethingHappened()
        }
    }
}

override fun onStop() {
    super.onStop()
    unregisterReceiver(myReceiver)
}
w3bshark
  • 2,700
  • 1
  • 30
  • 40
  • what should I use instead of `BROADCAST_SOMETHING_HAPPENED` for SMS recieving – Hasan A Yousef Mar 25 '18 at 19:45
  • 1
    @StevenElliott that would be answering Hasan's question and not OP's question. If you want to know what intent filter to use for SMS, then google it or post another StackOverflow question. But, there are already other StackOverflow questions on SMS filters, so here you go: https://stackoverflow.com/questions/8847876/android-sms-intent-filter – w3bshark Jun 14 '18 at 20:48
  • I have a hard time getting this syntax to compile. – JohnyTex Jan 20 '22 at 12:32