| Start |
|---|
| Using LocalBroadcastManager LocalBroadcastManager is used to send Broadcast Intents (opens new window) within an application, without exposing them to unwanted listeners. Using LocalBroadcastManager is more efficient and safer than using Here is a simple example of sending and receiving local broadcasts: |
| BroadcastReceiver Basics BroadcastReceivers are used to receive broadcast Intents (opens new window) 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 To receive the Intent, set the Action to something documented by Android OS, by another app or API, or within your own application, using Additionally, the Intent can contain information, such as Strings, primitives, and Parcelables, that can be viewed in |
| 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 Here I have taken an example of You can register a reciever in manifest file like this: Now device gets booted, |
| Enabling and disabling a Broadcast Receiver programmatically To enable or disable a Now we can call the following method to enable the Or we can instead use |
| Sticky Broadcast 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 |
| Using ordered broadcasts Ordered broadcasts are used when you need to specify a priority for broadcast listeners. In this example Furthermore broadcast receiver can abort ordered broadcast: in this case all receivers with lower priority will not receive a broadcast message. |
Bluetooth Broadcast receiver add permission in your manifest fileIn your Fragment(or Activity)
Register broadcast
Unregister broadcast
|
| BroadcastReceiver to handle BOOT_COMPLETED events Example below shows how to create a Also, you can use NOTE: The user needs to have started the application at least once before you can receive the AndroidManifest.xml MyCustomBroadcastReceiver.java |
| 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 (opens new window) is a way to send or receive broadcasts within an application process. This mechanism has a lot of advantages
A simple example of a LocalBroastManager is: SenderActivity ReceiverActivity
|
| 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. Activity A 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. |
| 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 Scan the manifest of the installed app, and send an explicit broadcast to to each receiver: |
Read Article
https://develop.horje.com/learn/1434/reference