Saren Tasciyan
Saren Tasciyan

Reputation: 483

How to have a service to run eternally on Android?

Search engines and Android developer website didn't help and I guess you can help with my problem.

I want to make an app for personal use, which is supposed to run all the time on my old tablet (powered all the time). The app will have several features requiring user interaction but independent of those, it should run a background job to check something continuously (real time!) for instance sound detection. It should also always try to connect another device on the network. That means that job needs to run almost eternally without being killed. Some comments I have found suggested AlarmManager or BroadcastReceiver. But those are triggered by very defined triggers (either time or broadcast). I don't want that, because it should perform its task continuously all the time. This background job should also be able to communicate with the main Activity of my app to report what it is doing and allow user to interact with it (change settings of the job for instance).

Do you know any way how to accomplish this? Is IntentService correct choice for this (hoping that it won't get killed or maybe I should let the Activity to restart it?)

Thanks!

Upvotes: 0

Views: 45

Answers (1)

CommonsWare
CommonsWare

Reputation: 1006614

Do you know any way how to accomplish this?

Build your own custom ROM, with a modified version of Android that contains your code as a native Linux daemon.

Otherwise, what you want is technically impossible.

You can come fairly close by using a foreground Service (not an IntentService) and returning START_STICKY or START_REDELIVER_INTENT from onStartCommand(). Android may terminate your process from time to time, but it should restart your service automatically after a short while. That service can use its own background threads to do whatever it is that you are trying to do.

Upvotes: 1

Related Questions