5

I am trying to understand how broadcast receiver works internally.

What happens internally when I call registerReceiver() method? Does it create a service internally ?

What happens internally when I call sendBroadcast?

If I have to implement my own BroadcastReceiver class (without extending the Android BroadcastReceiver class) how can I achieve it?

I did a lot of research, but I only found how BroadcastReceiver works. I looked at the Android source code too to find out how it works, but it didn't help either.

Cristian Ciupitu
  • 20,270
  • 7
  • 50
  • 76
Abdul
  • 79
  • 1
  • 6
  • "What happens internally..." -- IMHO, that is beyond the scope of a Stack Overflow answer. I suggest that buy a book on Android internals, such as [Karim Yahgmour's](http://shop.oreilly.com/product/0636920021094.do). – CommonsWare Jan 12 '16 at 22:23
  • If i have to implement my own BroadcastReceiver is there anyway ican achieve it ? – Abdul Jan 13 '16 at 04:47

2 Answers2

0

You wrote that you need to implement a BroadcastReceiver without extending Android BroadcastReceiver. This is not possible. The Android framework handles the dispatching and delivery of all broadcast Intents to all components that have registered themselves as being listeners for those Intents. When some application calls sendBroadcast(), Android checks if any BroadcastReceivers have registered as listener for that Intent and if it finds any, it instantiates the component (if it isn't already instantiated) and then calls the component's onReceive() method.

If you don't extend Android's BroadcastReceiver you cannot register your component as a listener and Android will not call your class' onReceive().

NOTE: Please explain what it is you really want to do if you want more help.

David Wasser
  • 93,459
  • 16
  • 209
  • 274
0

I'm not an expert in Android... but expert in everything else :P haha just kidding, i think that a BroadcastReceiver it's like a kind of (linux) crontab, it seems like Android already have some kind of piece of software (or internal service) running for this, and when you use it... you "program it" (to that "service") with selected filters and stuff, that's why is being called even if your app isn't running. OR MAYBE i'm TOTALLY WRONG :) but that's how i see it.

mzalazar
  • 6,206
  • 3
  • 34
  • 31