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).
Related
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
I've registered the Intent.action_screen_off/on in my oncreate method of my application, and i can receive the events when my application is running. But when my application exits i get an broadcast intent leaked exception (off the top of my head i think thats what its called, either way its caused by not unregistering my receiver when my app exits)
How can i receive these events when my application is not running? i'd prefer not to use a service or something like that, i've seen other apps do it without services or background processes.
i've seen other apps do it without services or background processes.
That's not possible. The only way to register a BroadcastReceiver other than in running code is to declare it in the AndroidManifest.xml with the relevant <intent-filter>. As it's not possible to do that for either Intent.ACTION_SCREEN_OFF or Intent.ACTION_SCREEN_ON, the apps you've seen MUST have some running component(s) in order to receive the broadcasts.
In short, use a Service - I'm not quite sure why you say you'd prefer not to.
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 fairly new to Android development so I was wondering how I should design/structure a new program I want to develop. I'm not sure if it should be started as a service or a broadcast receiver or an activity even (all I have ever done).
What I want to do is receive when certain events happen on the phone such as when the following are turned on blue-tooth, wifi, gps and camera. And when that happens send a message to a network manager (using SNMP).
Now I want this to continually run in the background waiting for these events to happen. Do I create a service and create receivers to create receivers and when the action happens create a service/tread to act on it/generate the SNMP message?
When that Is answered I was also struggling with how the manifest file would be structured fro when running receivers and services within the same set of code. But I guess that depends on how you design it to run?
All you need is a broadcast receiver which gets notified about the listed events and start a server who asynchronously send some messages.
I have an app which consists of a simple GUI and a few Broadcast receivers.
I found somewhere on here how to start an app on boot of the device but cant seem to locate it atm.
However i need more i need it to start in the background and just listen using the broadcast receivers and then return to this state after the Gui(the visible part of the app) has been used.
Is there a way of doing this?
Thanks.
Look into the Service class. Services run in the background and have no direct user interaction.
http://developer.android.com/reference/android/app/Service.html