-2

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

Lincoln White
  • 639
  • 2
  • 12
  • 29
  • @Hemant Parmar There is only talking about reminders, am asking about exact situation why we need to use these two types of broadcast receivers. can you able to explain? – Baskaran Veerabathiran Feb 28 '18 at 06:47

2 Answers2

0

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.

Muhammad Saad Rafique
  • 3,158
  • 1
  • 13
  • 21
0

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.

panchamk
  • 9
  • 5