I want to watch for a particular SMS, and handle it in a Receiver when it arrives. I then want to "eat it" so that it doesn't bubble upwards and display to the user (it should be handled "silently"). Is this possible? If so, how?
Is this possible? If so, how?
Since the SMS broadcast happens to be an ordered broadcast, your BroadcastReceiver can call abortBroadcast() to stop it from being handled by lower-priority receivers.
Here is a blog post from a while back discussing ordered broadcasts. Here is a sample project based upon that blog post. Here is a sample SMS BroadcastReceiver that conditionally executes abortBroadcast().
You are looking for the Service construct in Android. It is designed to run something, without requiring a UI (like an Activity).
BroadcastReceiver is additional functionality you should research to catch the SMS event.
I then want to "eat it" so that it doesn't bubble upwards and display to the user (it should be handled "silently")
Huh? Intents that you register to receive are handled silently unless you choose to handle it "loudly".
EDIT
Also, there is no way to prevent other apps from responding to SMS messages. Think about the security implications of allowing one app to control whether other apps can listen for system events...
Yes, it can be aborted by abortBroadcast() method and if you set priority of IntentFilter to 1000(highest priority) then this receiver will receive broadcast before the system.
Related
I was going through the docs on Broadcasts and found the following in the best practices:
When you register a receiver, any app can send potentially malicious broadcasts to your app's receiver. There are three ways to limit the
broadcasts that your app receives:
But receiving a broadcast entails receiving an Intent, carrying some data optionally. I was wondering how it can be malicious after all?
if you have listeners waiting for a specific signal, from that broadcast, so they can, for example, show a notification, someone can do a malicious app that can call that listener and star showing n number of notifications and start annoying your user.
I want my app to listen for intends broadcasted by the call application, and when a call intend is broadcasted for a specific number I want to launch a dialog. I read that "A broadcast receiver may not display dialogs, and it is strongly discouraged to start an activity from within a broadcast receiver" https://developer.xamarin.com/guides/android/application_fundamentals/broadcast-receivers/ so I am assuming I should instead make the broadcast receiver launch a service that then launches a dialog. Can anyone confirm this? Also any simplified examples would be highly appreciated
Thanks!
In the example below, the app uses a BroadcastReceiver to detect a phone call number and decide whether it should answer or not:
How to reject incoming call programatically in android?
So using a BroadcastReceiver for that isn't that bad.
If you just want to show information about the phone call, you can display an Notification, as suggested by Jon Douglas in the comments. Displaying Dialogs from BroadcastReceivers isn't allowed (also disencouraged).
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 .
I have a program that has a broadcast receiver that listens for Phone_State and then sends a user defined intent.
Well, my problem is that the system also sends out an intent (the one that I am trying to replace with my program) .
So I am trying to find a way to CANCEL the systems intent.
I have found that if i have a timer just wait for a little bit, then I can send mine after the systems, but that is not very good, and sometimes defeats the purpose of my program.
Also, i cannot set my program as a default because it is not a full dialer program. Just one action of it.
Someone please help me find how to listen for and cancel a system intent/activity....
Someone please help me find how to
listen for and cancel a system
intent/activity
You cannot "cancel" an activity, period.
You cannot replace the dialer.
If the system Intent was sent via sendOrderedBroadcast(), then you can call abortBroadcast() from your BroadcastReceiver, and any lower-priority receivers will not get the broadcast. However, I have no evidence that ACTION_PHONE_STATE_CHANGED is an ordered broadcast, and I sincerely hope it isn't.
Whatever you are trying to do probably should be accomplished via modifications to your own custom firmware.