I have a question about BroadcastReceiver in Android, say, about system broadcast receiver BOOT_COMPLETED. If I have written a listener to Broadcast after system is booted and installed it. How the android system would know that it has to notify my application, since my application is not running but just installed? Is it that, all BroadcastReceiver derived classes are always in the memory (or system loads them in the memory after bootup) and whenever any broadcast happens, all relevant application can receive it.
Thanks
Braj
When the broadcast BOOT_COMPLETED is sent, the Android system checks the manifests of loaded apps to see if anything is meant to handle that message. The same is true for implicit intents, and all broadcasts.
Manifests are public facing and allows the system to know what your app can and will do.
Related
I want to get broadcast for incoming calls in my android device. and for this I have used the broadcast receiver with action PHONE_STATE & register it statically in AndroidManifest.xml as per below added screenshot :
This broadcast is working fine till my app is in foreground or active in recent apps. But It's not working after my app gets killed from recent apps.
and this broadcast action has exempted from limitation of broadcast mentioned in this link: https://developer.android.com/guide/components/broadcast-exceptions.html
I have tried by registering this broadcast from my Activity & also by registering it statically in AndroidManifest.xml file But still I am not able to get any broadcast after my app killed from recent apps.
I want to get broadcast every time for all the incoming calls even after my is not active or got killed from receiver.
Please give some solution to achieve this. or
Please suggest me any other solution or any other broadcast through which I can achieve this.
I'm new to android and trying to build a simple app which needs to listen for incoming sms. I know that I need to use the BroadcastReceiver class and I also know how to make my own broadcast receiver. But how do I start it? Does it start automatically if I set the code for it in the manifest? The app just has the Main activity, do I need to somehow add a broadcast receiver in the onCreate of this activity? I searched for an answer, but it's still not clear to me. I know it's not nice to ask, but it would be great if you can share some sample code. Thanks!
If you declare the receiver within your AndroidManifest.xml, then you shouldn't need to do anything more. When a broadcast gets sent, the Android system will look through all installed apps and notify each app that has declared the appropriate Receiver in its manifest, starting the app in the process if necessary. For most cases, such as SMS, that is how you want to declare receivers, because most broadcasts are sent with the intent that you want to open your app when its not currently running to react to the broadcast.
Alternatively, you may declare the broadcast within a running activity, which may be useful if you want the broadcast to directly update the UI in your running app.
BroadcastReceiver Documentation
Well, there are lot of threads on this topic but all are before the release of android 3.1.
Now broadcast receivers will not work until user manually launches the application, i.e for broadcast receivers to work, the application should be in running state not stopped. There are certain questions in my mind right now, specially about BOOT_COMPLETED receiver.
Why would they still want to have BOOT_COMPLETED and won't let application use it? I mean there seem to be no point in having it. When system boots, apps are in stopped state and no app will receive this event if I'm not mistaking.
And on the developer page I read this:
"The platform defines two new intent flags that let a sender specify whether the Intent should be allowed to activate components in stopped application.
1:FLAG_INCLUDE_STOPPED_PACKAGES — Include intent filters of stopped applications in the list of potential targets to resolve against.
2:FLAG_EXCLUDE_STOPPED_PACKAGES — Exclude intent filters of stopped applications from the list of potential targets"
Can someone please explain the meaning of it. Can I still receive broadcasts when my app is in stopped state? And how can I register such receivers in manifest.xml ? I know these flags are added in the code but can I do similar in the manifest.xml?
Apps are in the stopped state if and only if they have never been manually launched by the user. It the user has launched the app at least once, the app can register for an receive BOOT_COMPLETE messages at startup.
I'm currently working on a project where I analyze the SMS_RECEIVED-Flow.
According to the article [1] I simulated the SMS_RECEIVED-Broadcast with an explicit call to the service: com.android.mms.transaction.SmsReceiverService.
The problem is, that the Android-Emulator and my Nexus do forward
the generated BroadcastReceiver to the default SMS-App only and I do not know why.
I tried to catch the Broadcast with an statically registered receiver in another App and with an dynamically registered receiver (all receiver registered with the highest priority and the correct intent-filter). Both methods work fine with a normal SMS, but not with my simulated one.
So the question is, does anybody know how the SMS-Receive mechanism work and why my program won't receive any broadcasts.
[1] http://blog.dev001.net/post/14085892020/android-generate-incoming-sms-from-within-your-app
SmsRecieverService is part of default messaging app, so if you explicitly call this service, the broadcast will reach only that app. Since the SMS_RECIEVED broadcast is a protected broadcast , your app cannot broadcast it.
One way is to change the name of the broadcast in both simulator and your app.
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.