In which situation do we have to use static broadcast receiver and in which situation do we have to use dynamic broadcast receiver in android?
Please Explain with examples
In which situation do we have to use static broadcast receiver and in which situation do we have to use dynamic broadcast receiver in android?
Please Explain with examples
Basic difference is that in static we use tag in Manifest file. All events can not be registered statically because for some events permissions are required so we register them dynamically on runtime. Moreover scope of application also matters if you need it in single activity for a single task then why to register that statically. But for some receivers like if you want your user to logout if he dont have internet we register that statically at application level because we need our user to stay connected.
You can set BroadcastReceiver
in two ways in Android:
One by simply using <receiver />
in the Manifest file.
The other way to do this is by calling registerReceiver()
method on your Context object. The registerReceiver()
method takes in two parameters: receiver
(The BroadcastReceiver you want to register and the filter
(The IntentFilter object that specifies which event your receiver should listen to.)
By doing the second method, your BroadcastReceiver
lives for as long as the component lives and Android sends events to this receiver until the creating component itself gets destroyed.
So choose the dynamic method if you want to optimize the performance of your app.