0

I've been looking through many questions about services, but I couldn't find one that suited me. I need a service that both starts on BOOT_COMPLETED (not bound to an Activity) and runs ALL the time (therefore I can't user AlarmReceiver). I know it might drain my battery but so far I don't care. It is just for research purposes.

I have a service that monitors sensor's data. What I managed to do so far was: either start the service as a regular Activity, but it runs only for +-20s and it is stopped (I think the SO cuts it down to release its memory); or start a service that runs in foreground. It worked to keep the process running, however the class that actually runs the service somehow was not started, besides an annoying notification which is required.

The code I refered as the one that runs the service in foreground was taken from here: Implement startForeground method in Android

I mean, how does an app like WhatsApp run constantly? Is it running in foreground? Because looking at Settings it seems the service is very stable, and it does not show any permanent notification, since it is not possible for a foreground service run without one. ( How to startForeground() without showing notification? )

Any advice?

Community
  • 1
  • 1
Teo Inke
  • 5,928
  • 4
  • 38
  • 37
  • Have you read about START_STICKY flag? – Merlevede Feb 25 '14 at 21:43
  • 1
    An app like whatsapp will not make it obvious when their service is not running for a few seconds or minutes. They probably have the same problem from time to time. – zapl Feb 25 '14 at 21:46
  • Yes, @Merlevede, but that's the point: START_STICKY just resets my service, however I'd like it not to be shut down/reseted all the time. – Teo Inke Feb 25 '14 at 22:15
  • 1
    WhatsApp certainly checks for new stuf every, let's say, 2 seconds. My point is, if you check your RUNNING processes, they keep running indefinitely. The only way I managed to do the same was to start a service running in foreground, which my not be the "proper" solution. – Teo Inke Feb 25 '14 at 22:18
  • There's NO WAY you can prevent your process to be killed (unless you're in the foreground). You have to be prepared and take appropriate action when restarting. – Merlevede Feb 25 '14 at 22:20

1 Answers1

0

You can use a WakeLock. But please remember, with great power comes great responsibility (to release them again and not over-use them).

But for now, just acquire a hefty WakeLock and only release it until you are done. This should keep your device's screen and CPU awake and allow you to do whatever it is you want to do.

Menno
  • 1,133
  • 9
  • 14
  • This is totally useless for two reasons: (1) A service might be running 24x7 (2) the user doesn't want the screen on – Merlevede Feb 25 '14 at 22:20
  • He says he wants a service that runs "*all the time*", so I don't know if your (1) is valid. (2) [Partial wake locks](http://developer.android.com/reference/android/os/PowerManager.html#PARTIAL_WAKE_LOCK). They don't keep the screen on, but keep the CPU running. – Menno Feb 25 '14 at 22:24