BroadcastReceiver (receiver) is an Android component which allows you to register for system or application events. All registered receivers for an event are notified by the Android runtime once this event happens.
for example, a broadcast announcing that the screen has turned off, the battery is low, or a picture was captured.
Applications can also initiate broadcasts—for example, to let other applications know that some data has been downloaded to the device and is available for them to use.
Android stopped state
Starting with Android 3.1 all applications, upon installation, are placed in a stopped state. While in stopped state, the application will not run for any reason, except by a manual launch of an activity, or an explicit intent that addresses an activity ,service or broadcast.
When writing system app that installs APKs directly, please take into account that the newly installed APP won't receive any broadcasts until moved into a non stopped state.
An easy way to to activate an app is to sent a explicit broadcast to this app. as most apps implement INSTALL_REFERRER, we can use it as a hooking point
Scan the manifest of the installed app, and send an explicit broadcast to to each receiver:
Bluetooth Broadcast receiver
add permission in your manifest file
In your Fragment(or Activity)
Add the receiver method
Call this method on onResume()
Call this method on onPause()
BroadcastReceivers are used to receive broadcast Intents that are sent by the Android OS, other apps, or within the same app.
Each Intent is created with an Intent Filter, which requires a String action. Additional information can be configured in the Intent.
Likewise, BroadcastReceivers register to receive Intents with a particular Intent Filter. They can be registered programmatically:
or in the AndroidManifest.xml file:
To receive the Intent, set the Action to something documented by Android OS, by another app or API, or within your own application, using sendBroadcast:
Additionally, the Intent can contain information, such as Strings, primitives, and Parcelables, that can be viewed in onReceive.
BroadcastReceiver to handle BOOT_COMPLETED events
Example below shows how to create a BroadcastReceiver which is able to receive BOOT_COMPLETED events. This way, you are able to start a Service or start an Activity as soon device was powered up.
Also, you can use BOOT_COMPLETED events to restore your alarms since they are destroyed when device is powered off.
NOTE: The user needs to have started the application at least once before you can receive the BOOT_COMPLETED action.
Communicate two activities through custom Broadcast receiver
You can communicate two activities so that Activity A can be notified of an event happening in Activity B.
Of course you can add more information to the broadcast adding extras to the Intent that is passed between the activities. Not added to keep the example as simple as possible.
Enabling and disabling a Broadcast Receiver programmatically
To enable or disable a BroadcastReceiver, we need to get a reference to the PackageManager and we need a ComponentName object containing the class of the receiver we want to enable/disable:
Now we can call the following method to enable the BroadcastReceiver:
Or we can instead use COMPONENT_ENABLED_STATE_DISABLED to disable the receiver:
Example of a LocalBroadcastManager
A BroadcastReceiver is basically a mechanism to relay Intents through the OS to perform specific actions.
A classic definition being
"A Broadcast receiver is an Android component which allows you to
register for system or application events."
LocalBroadcastManager is a way to send or receive broadcasts within an application process. This mechanism has a lot of advantages
since the data remains inside the application process, the data cannot be leaked.
LocalBroadcasts are resolved faster, since the resolution of a normal broadcast happens at runtime throughout the OS.
A simple example of a LocalBroastManager is:
Register a receiver
A concrete object for performing action when the receiver is called
unregister when the view is not visible any longer.
Introduction to Broadcast receiver
A Broadcast receiver is an Android component which allows you to register for system or application events.
A receiver can be registered via the AndroidManifest.xml file or dynamically via the Context.registerReceiver() method.
Here I have taken an example of ACTION_BOOT_COMPLETED which is fired by the system once the Android has completed the boot process.
You can register a reciever in manifest file like this:
Now device gets booted, onReceive() method will be called and then you can do your work (e.g. start a service, start an alarm).
If we are using method sendStickyBroadcast(intent) the corresponding intent is sticky, meaning the intent you are sending stays around after broadcast is complete.
A StickyBroadcast as the name suggests is a mechanism to read the data from a broadcast, after the broadcast is complete.
This can be used in a scenario where you may want to check say in an Activity's onCreate() the value of a key in the intent before that Activity was launched.
LocalBroadcastManager is used to send Broadcast Intents within an application, without exposing them to unwanted listeners.
Using LocalBroadcastManager is more efficient and safer than using context.sendBroadcast() directly, because you don't need to worry about any broadcasts faked by other Applications, which may pose a security hazard.
Here is a simple example of sending and receiving local broadcasts:
Using ordered broadcasts
Ordered broadcasts are used when you need to specify a priority for broadcast listeners.
In this example firstReceiver will receive broadcast always before than a secondReceiver:
Furthermore broadcast receiver can abort ordered broadcast:
in this case all receivers with lower priority will not receive a broadcast message.
This modified text is an extract of the original Stack Overflow Documentation created by following contributors and released under CC BY-SA 3.0