I want to start broadcast receiver class from my app’s main activity. Main activity has all the GUI stuff, this activity starts a service which performs all the work. For making this service start after reboot, I have written a broadcast receiver class. Now I am confused where to call broadcast receiver class.
- How can I call it from my main activity (I don’t want to make main activity a broadcast reciver)?
- If I call broadcast receiver from main activity, will it create a new instance of the broadcast class every time the activity starts. If so, will it cause memory leaks or the previous broadcast receiver instance will automatically be destroyed?
As a solution you may apply your own intent filter to
, register it and then call
in your activity just after registering the receiver. This suppose that you compare intents which comes to your receiver inside of
What about memory, it depends on how do you manage your activity’s lifecycle, because in a case of remaining references to your activity it will not be removed from the memory and so will cause creation of multiple instances of your receiver.
And you also may add
intent filter to start your receiver after device’s booting