I have an application I want to be an alternative handler for outgoing calls.
It would make sense to have an IntentFilter to handle CALL or CALL_PRIVILEGED intents. But that's actually not the right way to do this as doing so can interfere with EMS/911 calls. According to Google, you should write a BroadcastReceiver that looks for a NEW_OUTGOING_CALL. See following link:
http://android-developers.blogspot.com/2013/05/handling-phone-call-requests-right-way.html
So I've done this, but I want the behavior to be the same as if their were multiple activities with IntentFilters to handle the processing for the outbound call, i.e., I want a Chooser to come up for the User to decide which App he wants to handle the Intent.
Writing this the "wrong" way, I get this functionality for free. How do I get it by going via the BroadcastReceiver route?
Related
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).
I want to be able to detect when an application is opened and notify the user of something at the moment related to that same application but I don't know how to do this.
The user opens my app
I intent the service (background / foreground) and it successfully starts
Whenever the user opens another application I want to "catch it" and present a notification to the user
How can this be made? Are there any event listeners i need to use? Thank you very much.
If you are trying to catch "open app" intents in general, then it depends on how the app defined the intent. If it specified a class (explicit intent) then it will generally not be visible to your app unless the device is rooted, for example.
Implicit intents are broadcast and you simply need to define an intent filter in order to receive them. These are intents that allow Android and/or the user to select the appropriate app target based on data sent with the intent.
There are both useful and malicious motivations to do the kind of thing you are asking about. Read this:
Android Intent Security
And also the posted comment on learning about intents overall.
This is really simple. Here I am trying to figure out the solution. When your app goes on onPause() state then broadcast a message using BroadcastReceiver. On the other hand in another app just register for that broadcast.
I'm coming up to speed on Android development and the distinction between an implicit intent and a broadcast receiver is unclear. I was hoping for help in distinguishing these concepts and when to use the two.
Both receive intents, both react to system messages, so why is a broadcast receiver even needed and when is it used as opposed to an implicit intent and intent filter to accept the implicit intent?
Broadcasts are just that -- messages broadcast to anyone listening. They are inherently insecure, and delivery to the intended recipient isn't guaranteed, because there really isn't an intended recipient. For example, the CONNECTIVITY_CHANGE broadcast makes this quite clear: When connectivity changes in an Android device, many apps might be interested. Rather than the ConnectivityManager having to notify each app via specific Intent, it sends a broadcast. Any app that has registered interest in this event will be notified. Any app that isn't running or doesn't care... won't.
An Intent is "sent" when one app or Activity wants to launch another to do something very specific. For example, a file-manager might want to launch an image viewer or video player. Your app might want to launch a very specific Activity within another one of your apps, etc. The communication by specific intents (i.e. including package name and component name) can not easily be intercepted, so it's somewhat more secure. Most importantly, there's only and exactly one "receiver" -- if none can be found, the Intent will fail.
Further, a BroacastReceiver will be active within an Activity or Service and received broadcasts will generally only change state and/or do minor UI updates... for example, you might disable a few actions if your internet connectivity is dropped. By comparison, a specific Intent will usually launch a new Activity or bring an existing one to the foreground.
I am going to compile a list here of all the differences between Implicit Intents (sent via startActivity()) and Broadcasts (sent via sendBroadcast())
Broadcasts, by default, can affect multiple applications at once (Ordered Broadcasts have the potential to be disrupted). In contrast, Implicit Intents will only affect one application. Please note that there may be multiple possibilities of applications that could be affected, but eventually only one will be.
Implicit Intents are handled via Intent-Filters, and Broadcasts are handled via Broadcast Receivers (albeit the intent-filters play a role here too). I have seen in many instances over the web that Broadcasts are compared to Intent-filters and that does not make sense to me.
An Implicit Intent launches an Activity, or a Service. By contrast, a Broadcast launches a Broadcast Receiver. (This, if you think about it, is the core difference between Intents and Broadcasts. It is because of this reason that Broadcasts aren't meant to do too much heavy work, and especially not UI work!)
From the Developers Website:
There is no way for a BroadcastReceiver to see or capture Intents used
with startActivity(); likewise, when you broadcast an Intent, you will
never find or start an Activity. These two operations are semantically
very different: starting an Activity with an Intent is a foreground
operation that modifies what the user is currently interacting with;
broadcasting an Intent is a background operation that the user is not
normally aware of.
I'll add more if I find anything else.
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.
I have an Android service that runs in background.
I need it to recieve feedback (and stop, for example) just when user did something, no matter if it's only touch event on homescreen of somewhere else or key press.
Is it possible to handle global touch and key events?
While I highly doubt that there is an easy way to do this, you can register intents and intercept the messages for your own purpose. Have a look at the intent library for specific intents that you might hijack for your application. You may find further information in the android documentation on intent filters.