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
Related
I have a Broadcast Receiver that listens to app installation events.
What happens if multiple apps are being installed at the same time?
Will I get multiple calls or
Android might 'swallow' some of the calls and call me only once ?
Each installation should send one braodcast intent. The answer in here can help you. As said in there:
"If the BroadcastReceiver is registered in the manifest, a new
instance is created for each broadcast(...)"
I wrote program for broadcast receiver and service, but i confused in the manifest file there is some ground work to register service and receiver, will any one give me clear idea about this? Thanks in advance.
Service
It is used when you want to do something in background, any long running process can be done using Service in Background.
This will be running always in background even if the application closed
For example, you want to play music when your application gets close. In that case service will be running in background with music.
BroadcastReceiver
It is used when you want to fire some stuff or code during some event. For example, event can be on Boot of Device.
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
for example, If you want to perform something when device Boots, date and time changed etc.
A service is used to perform long running operations without user interaction or to supply functionality to other applications.
A Service needs to be declared in the AndroidManifest.xml via
a <service android:name="yourclasss"> </service> and the implementing class
must extend the Service class or one of its subclasses.
To start Services automatically after the Android system starts you can register
a BroadcastReceiver to the Android android.intent.action.BOOT_COMPLETED system
event. This requires the android.permission.RECEIVE_BOOT_COMPLETED permission.
For more details, check this http://www.vogella.com/articles/AndroidServices/article.html#pre_broadcastreceiver
A broadcast receiver is an Android component which allows to register for system or application events. All registered receivers for an event will be notified by Android once this event happens.
A Service is an application component representing either an application's desire to perform a longer-running operation while not interacting with the user or to supply functionality for other applications to use.
It'll be very long to explain it all here
I've got 2 great tutorial links from vogella
Broadcast Receiver
Service
if you have further question after reading the tutorial feel free to ask me in the comment :)
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 am going to implement Download Queue for my application. One of its feature will be when Network get connected or disconnected. I want to notify my QueueManger(Service) to update items status and stop.
I know use of Broadcast Receiver and how to use it. My question is if my application is stooped (finished) will broadcast receiver will work? Will it notify application or not?
If not is there any way to achieve this functionality even when application is stooped.
If you register your receiver in the XML instead of programmatically, then it should be able to catch these even when the program is not running.
See question, same solution applies Intent action for network events in android sdk
If you have defined your broadcast receiver as <receiver> in AndroidManifest.xml and listening for android's default action strings, then Yes, it will work.
Otherwise, I would recommend you to make use of Service. It can run silently in the background and do all the logic you want. Your service can even be provoked automatically when the phone is restarted (if you code correctly).
I have a app which is a single activity app , it listens to many broadcast event , my question is that how can I ensure that I receive all the fired intent which i have added for my app even when my app is not in foreground and running in background ??
is there a certain method where
I should add my intent filter and
do the registerReciever ? I'm sure
it won't work if I keep it in
"Oncreate"
any ideas or help ??
It sounds like you want to be using a BroadcastReceiver to pick up the events when you are in the 'background'. This will pick up events which are actually broadcast without having to launch a UI.
You can set up the BroadcastReceiver and its intent filters in the manifest for your application, this way you dont have to rely on any java code being run to register the receivers.