tl;dr... Service is getting less runtime on newer API.
My android app was originally written targeting android 4.3 (API version 18) because the phone I had at the time was an older one. Now I have a shiny new phone with Lollipop 5.1.1 (API 22).
Initially tried just naively installing the app on the new phone and most of it still works. One noticeable difference is that the service that I'm using to do some long repetitive tasks in the background is no longer getting the same amount of runtime that it used to.
The service updates an "ongoing notification" item that simply shows that the service is running by updating a countdown from 300-0 and then starting again. On the old phone this works fine and runs without interruption even when the phone falls asleep or if I'm using other apps. That is the behaviour I want.
However on the new phone with the later API, the service seems to get almost no runtime at all. Judging by my countdown numbers it looks to be getting 5 seconds of runtime at intervals of a minute or two and then halts again for a while. It seems to get more runtime when the phone is asleep. The code fundamentally still works because the coundown is eventually getting executed but with repeated interruptions.
I changed my Android Studio settings to target this phone's android version specifically but that doesn't seem to have rectified the problem.
I'm still relatively new to Android and don't really know how to attack this problem.
It's a work project so I can't paste in any code here, unfortunately. I am using startForeground() to begin the service and the service itself uses an internal implementation of the Handler class to allow multi-threading within the separate process/task.