Search code examples
androidxamarinbroadcastreceiverxamarin.formslocalbroadcastmanager

Why does a BroadcastReceiver work without an instance?


In this tutorial a class derives from BroadcastReceiver. It then receives messages. How? This is just a definition of a class, not an instance of it!

And after we figure that out - how do we prevent this from happening so that we can use this class with a LocalBroadcastManager, limiting it to the app only? (Not with the same exact case as in the tutorial, of course, because that's a message that's not from the app.)


Solution

  • When you use annotations like [Service] and [BroadcastReceiver] etc, the Xamarin.Android compiler automatically adds the required sections into the generated AndroidManifest.xml, which in case of the [BroadcastReceiver] it starts working because of the [IntentFilter].

    You can see generated manifest at obj\Debug\AndroidManifest.xml