tags:

views:

213

answers:

3

I have a background Service which must run permanently. The service just has to interact with my Activity.

  1. How to i check on activity resume if the service is still running? Is there a better possibility than a static variable in the service class?

  2. Is it of advantage to use a remote service in a separate process(to extend service life time) so that when the activity process gets killed the service is still alive?

+3  A: 

I have a background service which must run permanently.

This is not possible. The user or Android will kill off your service at some point. Please reconsider your architecture.

How to i check on activity resume if the service is still running?

Generally, you don't.

Is there a better possibility than a static variable in the service class?

That will not work if the user or Android kills off your service.

Is it of advantage to use a remote service in a separate process(to extend service life time) so that when the activity process gets killed the service is still alive?

A remote service has nothing to do with whether the service runs after activities are destroyed. If you call startService(), the service will run, independent of any activities, until:

  • you call stopService() from an activity
  • the service calls stopSelf()
  • Android terminates the service
  • the user terminates the service via the Settings application
  • the user terminates the service via a "task killer" (Android 2.1 and earlier, at least)
CommonsWare
and what it want the user to inform the activity on reappearance if the background service is still doing its job?
Sney
@Snej: I do not understand your question, sorry.
CommonsWare
Sorry, my last comment was a little bit messed up until I realized it. The question was how to determine when the user relaunches the application whether the background service is still running or has been killed. By Macarse's answer an Intent can be thrown at the service. But how do can I find out whether the service was restarted or is still running?
Sney
Finally, I ended up using the suggestion made by hackbod on http://groups.google.com/group/android-developers/browse_thread/thread/8c4bd731681b8331/bf3ae8ef79cad75d
Sney
+2  A: 

Why do you want to know if the Service is running? If you need something from it, just throw an Intent and if it's not running it will start by the intent.

In relation with the second question:

Your service will not "die" when your Activity closes.

Macarse
I want to know it because the application is logging gps data and it's crucial to log permanently. So i want to let the user know through the activity if it stopped logging because of an exception or killed service.
Sney
I have the same concern as Snej.
gregm
@Sney: This is a hack, but you can try sending `Broadcasts` from the service to let everyone know you are alive.
Macarse
+1  A: 

start the service in startforeground()....it would increase the timespan of the service.

Rohan K