Can anybody tell what is the use of a BroadcastReceiver and give an example in Android?
Can someone give a time zone change example using a BroadcastReceiver?
I think the android developers documentation explains it pretty good:
A broadcast receiver is a component that does nothing but receive and react to broadcast announcements. Many broadcasts originate in system code — for example, announcements that the timezone has changed, that the battery is low, that a picture has been taken, or that the user changed a language preference. 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.
(see developer.android.com)
On your android system broadcasts are used as an instrument of notifications. As the name suggests, they are broadcastet through your whole system. With a broadcast receiver you can catch these broadcast notifications.
Think about the broadcast receiver as a normal listener. If you listening for free beer and someone yells "free beer here", than you will react on that :) Thats your real life broadcast receiver example :D
A broadcast receiver is a component which allows us to register for system or application events. All registered receivers for an event will be notified by Android once this event happens.
Android system periodically broadcast messages about things that are happening, such as the battery status changed, the Wi-Fi came on, or the phone’s orientation changed. You can pick up these state changes and perform actions after intercepting them and all this is done using broadcast receivers. Broadcast receivers receive and react to broadcasts generated by system or apps .
Related
Choose whether the broadcast receiver should be exported and visible
to other apps on the device. If this receiver is listening for
broadcasts sent from the system or from other apps—even other apps
that you own—use the RECEIVER_EXPORTED flag. If instead this receiver
is listening only for broadcasts sent by your app, use the
RECEIVER_NOT_EXPORTED flag.
this is that google explains RECEIVER_EXPORTED, RECEIVER_NOT_EXPORTED
link to explanation
when i want to use broadcasts sent from the system
google say i need to use RECEIVER_EXPORTED
but when i tested with "android.intent.action.AIRPLANE_MODE" and "RECEIVER_NOT_EXPORTED"
I received boradcast event.
can anyone explain that sentence?
There might be an error in their document or they changed their mind, because according to ContextCompat documentation, you can use:
RECEIVER_NOT_EXPORTED if you only want your receiver to be able to receive broadcasts from the system or your own app.
In my opinion, it's safer like this, because we don't want our receiver to be open to all applications just to get system broadcasts.
(edited) I found also this on the RECEIVER_NOT_EXPORTED documentation:
Has the same behavior as marking a statically registered receiver with "exported=false"
And when I look at receiver documentation, for the exported attribute, I read:
If "false", the only messages the broadcast receiver can receive are those sent by the system, components of the same application, or applications with the same user ID
So I'm confident enough that the implementation is coherent and there is a small error in the documentation
Android developer portal states broadcasts which can be registered in manifest itself, but it gives a caution as under
Even though these implicit broadcasts still work in the background,
you should avoid registering listeners for them.
One such broadcast which is exempted is as under:
ACTION_LOCKED_BOOT_COMPLETED, ACTION_BOOT_COMPLETED
Exempted because
these broadcasts are only sent only once, at first boot, and many apps
need to receive this broadcast to schedule jobs, alarms, and so forth.
If we should avoid registering them (as per the above caution), then what is the right approach for ACTION_BOOT_COMPLETED ?
Use case: Sync data with the server and show a notification to user (if any)
If you need your app started at boot time, the only way to do this is to have a manifest-registered BroadcastReceiver.
Hi I'm want to collect user health data. In iOS we have HKObserverQuery to observe.So when ever there is a change in health data for example, change in step count it will wake our app. Is there any thing similar in android for live updates. So I can setup monitoring for any changes.
I been scratching my head for few days. Thank you.
Broadcasts
Android apps can send or receive broadcast messages from the Android system and other Android apps, similar to the publish-subscribe design pattern. These broadcasts are sent when an event of interest occurs. For example, the Android system sends broadcasts when various system events occur, such as when the system boots up or the device starts charging. Apps can also send custom broadcasts, for example, to notify other apps of something that they might be interested in (for example, some new data has been downloaded).
You Could Use LocalBroadcastManager for your App when data changed it will called from your code
Then Register Reciever in manifest
send Broadcast locally fron your app to Broadcast Reciever when data changed in store Like HKObserverQuery
https://developer.android.com/guide/components/broadcasts.html
Yes, there is a CompletableFuture class in java to do so. You can read about it in Java's documentation
I want to know the difference between services and broadcast receivers, can anyone point out an example that can be observed on android mobile devices.
Thanks
Service: If you want to do something in background , this will be running always in background even if the application closed. You can create this in separate process and also you can give your service to other app if you want. Downloading any content or Music is good example
Broadcast Reciever: Usually system will send some info which can be recieved by your app if you would wish to ,by registering. And you can do something what you want when that thing happens by using onReceive method.
Example is the system will send BroadcastReceiver when new sms arrives or Booting done
Here is good article : Service and BroadcastReceiver
Service is used when you want to do something in background, any long running process can be done using Service in Background. For example, you want to play music when your application gets close. In that case service will be running in background with music.
Example of Service
BroadcastReceiver is used when you want to fire some stuff or code during some event. For example, event can be on Boot of Device. If you want to perform something when device Boots, date and time changed etc...
Example of BroadcastReceiver
I think of it possibly a different way. A Service receives intents that were sent specifically to your application, just like an Activity. A Broadcast Receiver receives intents that were broadcast system-wide to all apps installed on the device.
(The reason I say a Service is a bit like an Activity is that: You wouldn't broadcast a message saying "start Activity MyActivity" across all apps installed on the device. It is only for your specific app.)
Of course, as others mentioned, a Service can continue running in the background, whereas a Broadcast Receiver should finish quickly (e.g. if it is running for more than 5 seconds it may be killed by the OS). The Broadcast Receiver can still run in the background (when app is closed) under certain circumstances. For this, it's worth mentioning that there are actually two types of Broadcast Receivers - Manifest-declared, and Context-registered. They have different lifespans and restrictions - the former can receive broadcasts in the background with certain restrictions, while the latter cannot receive broadcasts in the background (app must be running and active) but has no restrictions on the types of intents that can be received.
Both services and broadcast receivers must be specifically invoked (via an intent), but for services this is usually a specific call (e.g. when your app is started or when the user clicks some button) whereas for broadcast receivers they don't need to be explicitly started as they will start anyway when a relevant broadcast is made.
Here's how I would think of it:
Type
Displays UI?
Can continue running for a long time when app is closed?
Can receive intents when app is closed?
Intents must specifically target your app?
Restricted list of intents that can be specified?
Activity
Yes
No
Yes
Yes
No
Service
No
Yes
Yes
Yes
No
Manifest-declared Broadcast Receiver
No
No
Yes
No
Yes1
Context-registered Broadcast Receiver
No
No
No
No
No
1: Only if you target Android 8.0 or above. The restrictions are not applied if the intent specifically targets your app. The restricted list of intents can be found here.
What is a BroadcastReceiver? What are its uses and how can I use it?
Start by reading the documentation. Also, copying from Application Fundamentals:
Broadcast receivers
A broadcast receiver is a component that responds to system-wide
broadcast announcements. Many
broadcasts originate from the
system—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.
Although broadcast receivers don't
display a user interface, they may
create a status bar notification to
alert the user when a broadcast event
occurs. More commonly, though, a
broadcast receiver is just a "gateway"
to other components and is intended to
do a very minimal amount of work. For
instance, it might initiate a service
to perform some work based on the
event.
A broadcast receiver is implemented as a subclass of
BroadcastReceiver and each broadcast
is delivered as an Intent object. For
more information, see the
BroadcastReceiver class.
Finally, read in Common Tasks how you can utilize BroadcastReceivers to listen for messages and set alarms.
A broadcast is generated by android on occurrence of some action , BroadcastReceiver class enables the developer to handle the situation on occurence of the event/action . Action can be arrival of msg or call , download complete , boot completed , etc.
Broadcast Receivers simply respond to broadcast messages from other applications or from the system itself. These messages are sometime called events or intents. For example, applications can also initiate broadcasts to let other applications know that some data has been downloaded to the device and is available for them to use, so this is broadcast receiver who will intercept this communication and will initiate appropriate action.
I like this slide, because it focuses on Broadcast Receiver and offers simple description. The minor problem is that the updated date was a little bit old ( in 2011 ).
Link
Android Application Component: BroadcastReceiver Tutorial
(retrieved from the slide)
Broadcast Receiver
Receives and Reacts to broadcast Intents
No UI but can start an Activity
Extends the BroadcastReceiver Base Class
BroadCastReciever is an Android Component that helps you to know handle registered System Events or Application Events.
For Example:
System Events Such us : 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... etc
In simple terms
A broadcast receiver is basically an interface that you can implement so that your app can subscribe to system changes like when the system has finished booting, or a charger is connected/disconnected or airplane mode is switched on/off etc.