2

This is how I'm sending broadcast

if (isPlaying) {
                val intent = Intent(MUSIC_REQUEST)
                intent.action = PAUSE
                requireContext().sendBroadcast(intent)
                binding.playSong.setImageResource(R.drawable.ic_play_icn)
            } else {
                val intent = Intent(MUSIC_REQUEST)
                intent.action = PLAY
                requireContext().sendBroadcast(intent)
                binding.playSong.setImageResource(R.drawable.ic_pause_icn)
            }

And this is how I'm receiving Broadcast

when (intent!!.action) {
                PAUSE -> {
                    if (mediaPlayer!!.isPlaying) {
                        Log.i(TAG, "onReceive: paused Received")
                        mediaPlayer!!.pause()
                        isPlaying = false
                        val pI = Intent(MUSIC_REQUEST)
                        pI.action = PAUSE_REQUEST_COMPLETED
                        sendBroadcast(pI)

                    }
                }
                PLAY -> {
                    isPlaying = true
                    Log.i(TAG, "onReceive: play Received")
                    mediaPlayer!!.start()
                    val pI = Intent(MUSIC_REQUEST)
                    pI.action = PLAY_REQUEST_COMPLETED
                    sendBroadcast(pI)
                }

This is how I'm registering it in on create of service

LocalBroadcastManager.getInstance(this).registerReceiver(receiver, IntentFilter())

but at receiving end I'm unable to receive intent. Kindly guide me what could be possible error

  • [Local broadcast is now deprecated](https://developer.android.com/reference/androidx/localbroadcastmanager/content/LocalBroadcastManager) , if it's a new project I suggest you migrate to [LiveData](https://stackoverflow.com/questions/62196382/how-to-use-livedata-in-place-of-localbroadcastmanager) – Nitish Jan 04 '22 at 05:04
  • @Nitish for now can you please guide me how to use this broadcast? – Muhammad Sajid Jan 04 '22 at 05:19
  • I haven't used local broadcast in long time, but as far as I remember there should be event name in `IntentFilter()` , othervise how will broadcast will know for which on event it's register. Here this example might help you - [Local broadcast android](https://stackoverflow.com/questions/8802157/how-to-use-localbroadcastmanager) – Nitish Jan 04 '22 at 05:34

2 Answers2

1

Broadcasts sent via requireContext().sendBroadcast(intent) are not local broadcasts

You need to use LocalBroadcastManager for the sending as well.

LocalBroadcastManager.getInstance(this).sendBroadcast(intent)
Nitish
  • 3,075
  • 3
  • 13
  • 28
0

register receiver like this:

val intentFilter = IntentFilter()
filter.addAction(PAUSE)
filter.addAction(PLAY)
context.registerReceiver(receiver, intentFilter)

or this: add the filter to your receiver, in AndroidManifest.xml

<intent-filter>
    <action android:name="PAUSE" />
</intent-filter>
<intent-filter>
    <action android:name="PLAY" />
</intent-filter>
Krahmal
  • 195
  • 13
  • I'm using local broadcast and I think no need for declaring in manifest – Muhammad Sajid Jan 04 '22 at 05:57
  • @Muhammad Sajid Yes. Dynamic registration is enough. Check your action name . Use your package name or something you like as prefix, e.g. ```const PLAY = "${BuildConfig.APPLICATION_ID}.action.PLAY"```. – Krahmal Jan 04 '22 at 06:06