Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamic Registration vs Static Registration of BroadcastReceiver

Tags:

All of us known we register BroadcastReceiver in two types

1)Static Registration

2)Dynamic Registration

But my doubt is when we need to use Static and when we need to use Dynamic?

like image 306
Krishna Avatar asked Apr 23 '14 10:04

Krishna


People also ask

What is the difference between static and dynamic registration of broadcast receiver?

The main difference in working between the static and dynamic receivers is that the static receivers will run if the application is running/not running. But the dynamic receivers will run if the application is running.

What is static and dynamic broadcast receiver?

Static Broadcast Receivers: These types of Receivers are declared in the manifest file and works even if the app is closed. Dynamic Broadcast Receivers: These types of receivers work only if the app is active or minimized.

How do I register a broadcast receiver dynamically?

This example demonstrates how do I register a BroadcastReceiver programtically in android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.

What file is used to register the BroadcastReceiver?

An application listens for specific broadcast intents by registering a broadcast receiver in AndroidManifest. xml file. Consider we are going to register MyReceiver for system generated event ACTION_BOOT_COMPLETED which is fired by the system once the Android system has completed the boot process.


2 Answers

As we know there are two ways to register a BroadcastReceiver; one is static and the other dynamic.

Static:

  1. Use tag in your Manifest file. (AndroidManifest.xml)
  2. Not all events can be registered statically.
  3. Some events require permissions.

Dynamic:

  1. Use Context.registerReceiver() to dynamically register an instance.
  2. Note: Unregister when pausing.

When we are doing dynamic registration (i.e. at run time) it will be associated with lifecycle of the app. If we do it static registration (i.e. on compile time) and our app is not running, a new process will be created to handle the broadcast.

like image 96
Jitesh Upadhyay Avatar answered Oct 12 '22 01:10

Jitesh Upadhyay


1) Static Registration

Implementation are in manifest, android system can initiate processes and run your boardcast receiver. One example like you want to update your data when a new intent coming in from system or etc.. You need to take care Security issue as well.

2) Dynamic Registration

Implementation are in java code, boardcast receiver runs only when your app is running up to that registration line. Thus, you mostly want to use this if you only want to bring up the boardcast receiver with certain conditions.

like image 28
phdfong - Kenneth Fong Avatar answered Oct 12 '22 00:10

phdfong - Kenneth Fong