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
Related
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.
I have an App which is a BroadcastReceiver and which processes NEW_OUTGOING_CALL intents. There are also other apps on my phone that are registered as receivers for these intents, but mine is registered with a higher priority intent filter, so my BroadcastReceiver gets to see the intents first.
I would like to programmatically be able to prevent any other registered BroadcastReceiver for NEW_OUTGOING_CALL from processing these intents, but I would like to allow the phone call to proceed. Is this possible?
I don't think you can actually do what you want to do. The documentation for this action describes pretty clearly how the system expects this broadcast to be handled:
For consistency, any receiver whose purpose is to prohibit phone calls should have a priority of 0, to ensure it will see the final phone number to be dialed. Any receiver whose purpose is to rewrite phone numbers to be called should have a positive priority. Negative priorities are reserved for the system for this broadcast; using them may cause problems.
If you want to see the number first, you can do that (with a higher priority), but then you have to live with other receivers seeing it after you do. Alternatively, you can see the number last (by lowering your priority to 0), but then you have to live with other receivers seeing the number before you do.
Note that another person suggested aborting the broadcast. Not only does this not make sense since the system needs the result of the broadcast, it also is stated explicitly in the documentation not to abort this broadcast.
Any BroadcastReceiver receiving this Intent must not abort the broadcast.
I need to check the vulnerability of my app. I am trying to intercept a broadcast message in android such that no other app is able to use that broadcast, is there any way for this?
No.
The closest you can come is if it is an ordered broadcast, if your receiver is higher priority than any other, you can abort the broadcast when you receive it. However, there is nothing preventing some other app from specifying an even higher priority, and where there is a tie (Integer.MAX_VALUE), there's a tiebreaker. I think the tiebreaker is first-one-installed wins, as that tiebreaker is used elsewhere.
If the broadcast is not ordered -- IOW, most broadcasts -- then you cannot abort it, and all registered receivers will receive it.
FWIW, I fail to see how preventing broadcasts being delivered to other apps helps "check the vulnerability of [your] app".
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.
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 .