1

I am trying to build a count down timer that runs in the background in a separate thread. I am using a foreground service and passing a handler to the background thread from this service. So essentially, the activity will communicate with the service and the service will communicate with the thread.

Essentially, when the user swipes the app off from the recent apps, i need the timer to keep running.

What is the best design that does this?

brainfreak
  • 137
  • 1
  • 13

2 Answers2

0

You will need to use a started Service and startForeground().

Check this question for details.

Also if you create service in a separate process it will solve your issue.

Here is a great tutorial

A started service can use the startForeground(int, Notification) API to put the service in a foreground state, where the system considers it to be something the user is actively aware of and thus not a candidate for killing when low on memory.

Community
  • 1
  • 1
Ivan V
  • 3,024
  • 4
  • 26
  • 36
  • startService() is a one-shot task. I need a client-service interface. Moreover, when the app is killed, any service associated with it is killed. – brainfreak Oct 04 '15 at 16:18
  • Does not matter. Use it and the system will try to restart the service. I need something such that even if the user manually swipes the app out of the recent apps, i need the service to run. – brainfreak Oct 04 '15 at 16:22
  • other that this, there is no way to keep service running when process the app that contained it was killed in android (cause service is running in the same process). – Ivan V Oct 04 '15 at 16:39
  • You can make the service run in a different process but my question is will doing that help in keeping the service alive after the app is manually swiped off? – brainfreak Oct 04 '15 at 16:45
0

Like Ivan previously said, it is a combination of startService(), binding calls and startForeground() call to keep the service running in the background. Don't really require a separate process to do this.

brainfreak
  • 137
  • 1
  • 13