My app relies on logging the battery temperature, as well as several other sensor variables, every second in a background service that can be started from the main activity and continues running in the background until it's stopped (by another button in the main activity). This works fine in Android 7.0 and below, but reading into Android 8.0, it looks like it limits background services pretty severely. Can I still do this data logging that I currently do, or is that no longer possible?
-
1it will run just fine as long as you show a foreground notification about the work you are doing from service :) otherwise, it will crash after a few seconds. – Somesh Kumar Mar 30 '18 at 05:06
-
Thanks for the reply! Is this the method I need to use to keep a background service running while showing the user a notification: https://developer.android.com/reference/android/app/Service.html#startForeground(int,%20android.app.Notification) – Yuerno Mar 31 '18 at 22:50
-
1Yes.. This is useful when your app is not running in background and you're doing some stuff in background. Your user deserves to know what you're doing in background. – Somesh Kumar Apr 01 '18 at 12:18
1 Answers
If your usecase is to keep the app in foreground during this processing then, based on the documentation, there shouldn't be any problem, since the restrictions for background service is applied if your app is not in Foreground or no foreground app is connected to the app
X minutes (based on my observations its around 1 - 2 minutes) after your app enters in background all the restrictions for background service will kick in and your Service will be stopped as if you have called Service.stopSelf()
You should avoid doing it continuously, since it will impact battery life of the device, if are intending to do in background. You can perform logging periodically using JobScheduler/AlarmManager.
If you still want to proceed, you can make the service ForegroudService and perform the operation but if system considers your task as CPU intensive then this approach wont' work either

- 23,903
- 4
- 62
- 62
-
Thanks for the answer. If I make the service a ForegroundService, does that mean I have to be within the activity of my program to have that service running? Or can I be using other apps and doing other things and still have my service running in the background somehow? – Yuerno Mar 31 '18 at 22:51
-
If you are running a foreground service, you can use other apps without impacting your intended usecase. Its similar to playing a music player. You can listen to music and use other apps. – Sagar Apr 01 '18 at 00:12
-
@Sagar, do you know if Android should call Service.stopSelf() when it kills the service? I have overridden it, and it does not seem to be called, the reason I am asking is because i have a few programs which rely on logging sensor data in the background, and some are still working fine, others just stop after a few minutes, and I can't work out why some still work in Oreo, and others do not – Geordie Wicks Sep 27 '18 at 00:47
-
@GeordieWicks The documentation doesn't explicitly states that `stopSelf()` will be called. Based on my interpretation, the behaviour for killing the service by the OS is similar to calling `service.stopSelf()` but doesn't mean OS will call `stopSelf()`. You can rely on `onDestory()` in this case. – Sagar Sep 27 '18 at 01:39
-
@sagar, interesting, onDestroy is not being called when my service is killed...maybe I have a different problem. Does it definitely get called when android kills it in your experience? – Geordie Wicks Sep 27 '18 at 01:55