I've declared a background service from the android manifest, this service is to be started from within the main Activity of the application and run in an infinite loop to perform location updates to an external server.
This background service runs in a separate process than the process of the main activity of the application (using the android:process tag in the manifest declaration of the service). The issue I am having is that when requesting GPS location updates from within the service, then the LocationListener's onLocationChanged() method is never called, even when I send mock location using DDMS/geo command. But on the other hand, requesting GPS location updates from the main activity of the application does work and the onLocationChanged() method is being called properly when a fix is received, so I am wondering what is needed to be modified in order to get the same behaviour from within a service rather than an activity.
Thanks in advance for your help;
Hass.
this service is to be started from within the main Activity of the application and run in an infinite loop to perform location updates to an external server.
Please don't do that, as you waste memory. Please use AlarmManager to trigger a self-stopping Service (like an IntentService) periodically. And, since you appear to be getting locations, consider using my LocationPoller service.
This background service runs in a separate process than the process of the main activity of the application (using the android:process tag in the manifest declaration of the service).
Please don't do that. You waste memory and add no value. It may also be contributing to your problems.
so I am wondering what is needed to be modified in order to get the same behaviour from within a service rather than an activity.
Getting locations within a service works just fine. See the LocationPoller for an example of this in use.
Related
I am getting confused between service and Intend service,what is the difference between service and Intend service,then on which kinds of situations we have to use service,Intend service any can explain with some real time example?
Thanks in Advance
A Service is a piece of code that will run on your main UI thread and will remain running until stopped, even if you aren't in the foreground.
An IntentService is a special kind of Service that starts its own Thread and queues incoming start calls to run on that thread one at a time, in the order they came in.
Use Service if you need a place for long running actions to occur that need to continue even if the Activity is killed. For example, tracking location via GPS for a maps app. Use IntentService for repetative pieces of work. For example, downloading files. Or syncing a database. You can think of IntentService as kind of like an AsyncTask that runs in a Service.
I need some explanations about IntentService. I have to monitor location in background. I know how to receive location updates while the app is running, but my question is: should I put all the code I used before, in the IntentService?
For example, if I want to monitor the location changes, and apply some changes when the location is actually changed (not to the app UI, but to the system, for example the phone wallpaper), should I do it directly in the IntentService?
You should use Service (Sticky) rather using IntentServiceBecause IntentService is creates an internal worker Thread itself and as soon as task within IntentService completed its stop itself but in your case you need get Location continuously in background.
So in this case you should crate service with Thread in side your Service and its working all time.
Ive created an app which needs to run an intent service while the application is closed if two values are out of sync(eg. 2 of 3). The intent service updates a text view within my application which will display the two values using a BroadcastReceiver.
Also the intent service is created in one activity but should be stopped in another. The two values are based on a GPS location update and that location being sent through a web service method. At points the GPS may update location and the web service might not due to limited connectivity to mobile data/wifi therefore leaving the values out of balance(1 of 3) and if the user closes the app this needs to continuously run until the value reaches 3 of 3(the GPS stops updating when app is closed).
The intent should run until the values are in sync again even if application has been closed. How would i keep this running whilst the app is closed and then stop it when the the values are in sync
Because an IntentService is like a Thread. It's designed to run aslong as it takes to finish onHandleIntent().
Use a regular Service and control it with stopService or stopself() if you want to manage the lifecycle.
edit: by the way, why do you mix asynctask and intentservices at all? Use one of them but dont mix it. AsyncTask handles all. Background Threading and processing in the mainUI. Anyway, for using GPS you should decide to use a regular Service and (maybe) create a Thread within the Services onStartCommand().
I have a service in my Android application that needs to continue listening for location updates after the user has exited the application (the implications of this on battery life are a separate matter).
I'm not sure that I have correctly understood the lifecycle of a service in Android, outlined on this page:
http://developer.android.com/reference/android/app/Service.html
I believe if it returns START_STICKY in the onStart() method then the service will continue to run after the main application has quit regardless of whether or not the service is running in its own process. If the service is running in the same process as the rest of the app and I have understood correctly, the main app's process is kept alive after the app exits, just to run that service. When the app starts again, it will run in the same process as the service, which is still running. If the system gets low on memory, Android may decide to kill this service.
Secondly, I believe it is OK to run the location listener listening for GPS updates in the same process and indeed same thread as the rest of the application and it will not block when waiting for updates from the GPS.
Have I understood correctly?
You have understood correctly.
If the system gets low on memory, Android may decide to kill this service.
If you want it to be persistent you can create persisitent service (but it needs to be a system app / service).
Please use AlarmManager and an IntentService, so your service does not need to be in memory except when it is doing meaningful work. This also means Android is rather unlikely to kill your service while you are in memory, and users are unlikely to kill your service because they think you are wasting memory.
For your location Listener:
Use the Location Listener implemented in a service.
Start listening the GPS when the service starts and remove the GPS listener when the service stops.
Start this service when you wants to listen to GPS(every 10 minutes for example).
This is cleaner than having a service you try to continuously run and checking for Location changes.
Secondly, I believe it is OK to run the location listener listening for GPS updates in the same process and indeed same thread as the rest of the application and it will not block when waiting for updates from the GPS.
You do not need to setup a AsyncTask or background thread for this. Also understood correctly.
The actual life cycle of Service is described here by image. You run whatever service in android the life cycle if always follow like this.
I'm in little bit confusion where in what case I need to use application components like Service, asyncTask and Broadcast Receiver.
Can any one explain what the exact difference between these there and where I need to use these components?
AsyncTask is a friendly way to create a new thread that performs some work asynchronusly.
A Broadcast Receiver is something like an Event Handler for system events. It can run in
background and perform an action when something happens, like turning the phone off or turning wifi on..
A Service is just an app that works in background (like a daemon) and serves information to an app or just performs tasks.
Sorry for my English, I try to let me understand but it is not my mother tongue
I will get straight to where I have applied these three in my projects so far:
1.Service:Something you want to perform in the background without any user interaction.For instance fetching location data continuously or sending some data continuously to your server.You can also use services to perform tasks every few time units.For example sending ten minute background updates.
2.AsyncTask:Making a new thread of execution.Best use I have encountered so far is calling a web service..I did the following using an AsyncTask for web service calls
1.Display Progress bar in onPreExecute()
2.Perform my web service calls in doInBackground(Params...)
3.In onPostExecute(Result) update the UI or do some other stuff with the response from the web service.
3.BroadCastRecievers are like global recievers for your app.They can listen for both System events like a phone restart or a custom event within your app.I used them for starting a service when the phone was restarted,which stopped when we switched off the phone.
Let me explain with a usecase, so you understand it better -
AsyncTask - Want to get something from the server, or post something to the server? If we do so on the main thread, the user won't be able to interact with the app. So Asynctask is used, and it performs the network activity in a different thread.
Service - Want to manage something in the background? Like get the users' location every 10 minutes or 1 hour, or alert the user when he is crossing a particular area based on the location. The Service makes the app run even when the app is not opened (the user might be doing something else, or the phone is locked, the Service still runs in the background).
Broadcast Receiver - Assume, you are tracking location and storing locally (when the internet is down). Not when the internet is up, you want to send all of them. So you register with the OS, that you want to listen for that specific event, and you get control.
Or when you want the server to know that the device is restarted, then we just have to implement it.
Clear?
A service and its local memory-variables are loaded into memory and is always running
A BroadCast receiver is only garanteed to be in memory and running while processing an event.
A Broadcastreceiver can be removed from memory by the operating system if the memory is low.
"Service" is a component which runs in the background, without interacting with the user. Every developer can create new Services in his application. Services support true multitasking for Android, as they can run in their own process.
"AsyncTask" encapsulates the creation of Threads and Handlers. An AsyncTask is started via the execute() method.the execute() method calls the doInBackground() and the onPostExecute() method.
Mostly main purpose to download something without user interaction.
"Broadcast receiver" is a class which extends BroadcastReceiver and which is registered as a receiver in an Android Application via the AndroidManifest.xml file(or via code).you can register a BroadcastReceiver dynamically via the Context.registerReceiver() method.
The class BroadcastReceiver defines the onReceive() method. Only during this method your BroadcastReceiver object will be valid, afterwards the Android system can recycle the
BroadcastReceiver.