I would like to build an android app with a permanent background process. I thought of using a Job, but is there a better class for doing this?
-
1What have you researched so far? Can you explain which 'background process' could be the solution? BTW, do you know foreground services? – Taseer May 03 '19 at 05:56
-
Have a look at [this documentation of an `IntentService`](https://developer.android.com/training/run-background-service/create-service). Maybe that's suitable for your requirements. – deHaar May 03 '19 at 06:07
-
Foreground service, probably – EpicPandaForce May 03 '19 at 09:50
-
Is there a reason you want permanently access to execute code? There's a reason why manufactures choose to kill apps: they use way too much battery even when closed. – Zun May 03 '19 at 10:57
1 Answers
A service may be what you're looking for. These always run in the background, however have many limitations and are often killed by the OS.
If you need your service to (almost) always be alive, it can either be woken up by system intents / notifications, or run as a foreground service (notification is shown to user, but app is almost always active).
From the above link:
Foreground
A foreground service performs some operation that is noticeable to the user. For example, an audio app would use a foreground service to play an audio track. Foreground services must display a Notification. Foreground services continue running even when the user isn't interacting with the app.
Background
A background service performs an operation that isn't directly noticed by the user. For example, if an app used a service to compact its storage, that would usually be a background service.
As mentioned in comments, IntentService
is another option, here's a comparison between it and a normal Service
. It will however suffer the same restrictions as a normal background service.
For a more accurate answer, I'd suggest editing your question to include what your service needs to do.
-
IntentService cannot be started from the background as of Oreo restrictions, best choice would probably be a sticky service that gets restarted on reboot or whenever necessary, if it needs to complete some long running task/work without needing any restarts or such then I would say JobIntentService – Brandon May 03 '19 at 10:22
-
@Brandon Got it. I was including it as a commenter mentioned it, have never used myself. Will update. – Jake Lee May 03 '19 at 10:34
-
I am trying to develop an app, which permanently executes my code every second. It would also be ok, if it needs root. – Venji May 03 '19 at 14:01
-