I am trying to register a class that extends BroadcastReceiver as a receiver in Android manifest. I have no trouble registering them but the problem occurs because of the class does not have an empty constructor.
TLDR; How to implement a class that is a broadcast receiver (requires empty constructor to register it in android manifest) but at the same time, be a singleton class or a class that denies users access to the default constructor. (I've tried making the default constructor protected but that does not solve the problem as the manifest cannot register the receiver)
is there a way around this?
No. Android has no idea how to invoke any other constructor, or what values to pass to that constructor.
this class is also a singleton class
That is not possible. Android will create a new instance of your manifest-registered BroadcastReceiver
for every broadcast that it receives.
but there has to be a simpler method right?
Yes: do not make the BroadcastReceiver
a singleton. Make some other class be the singleton, that the BroadcastReceiver
uses.
BroadcastReceiver
is an abstract class, which a class must extend in order to be registered as a receiver
on your Manifest
. You cannot change the scope of its methods once you extend it and you cannot enforce a Singleton
pattern on it. If you are trying to set a Singleton
as your BroadcastReceiver
, there may be an issue with your design.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With