0

all senior android developers. I am facing an issue in android oreo devices. I have developed an android app which continuously gets user location during the trip. The app runs continuously for 5 to 10 hours or maybe sometimes more. The drivers are advised to keep the app open. A foreground service runs on all devices and notification get updated after every location update. Location update period is 10 seconds. But after sometime service get stopped on oreo devices. i also used getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON) Inside activity which starts service. I have also used a wake lock to keep CPU on. Ignore battery optimization is also used for oreo devices. This location service example code is used for service. If anyone can suggest a better solution.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
  • for services you should use work manager now – Suraj Vaishnav Mar 05 '19 at 12:02
  • Do you think work managers are better to use location after every 10 seconds? – Sulaiman Malik Mar 05 '19 at 12:08
  • it may take more than 10 seconds but it is recommended to use WorkManager, i don't think your service will run on>8.0, as the problem you are facing. – Suraj Vaishnav Mar 05 '19 at 12:09
  • As per developer [document](https://developer.android.com/about/versions/oreo/android-8.0-changes#back-all) android has imposed certain limits of background tasks. So you need to use other services like [this](https://codelabs.developers.google.com/codelabs/background-location-updates-android-o/index.html?index=..%2F..index#0) or [this](https://stackoverflow.com/questions/48265730/in-oreo-8-0-0-api-26-how-to-get-a-location-services-update-when-the-app-i) – Jaydroid Mar 05 '19 at 12:11
  • @Jaydroid i have used the code you suggested in links. None of them will resolve problems on oreo devices as system either kill service or app or both after sometime when power button is pressed – Sulaiman Malik Mar 05 '19 at 12:21
  • @SurajVaishnav i want to use work manager. but i wonder how they behave? for example in my case service started when trip started and destroyed when trip ended. can we use work managers this way? – Sulaiman Malik Mar 05 '19 at 12:22
  • How do you know it's not crashing? I write foreground services that run for months at a time, no problem. – greeble31 Mar 06 '19 at 02:22
  • how do you tested it's running continuously? can you share some line of codes to make it unique? – Sulaiman Malik Mar 06 '19 at 06:18
  • @SulaimanMalik Well, among other things, the notification bar item will contain (by default) a line indicating how old the notification is, such as "34d" for "34 days" (on Pie) or the start date (Nougat and earlier). I also use a custom `Thread.UncaughtExceptionHandler` to make a log entry in the event the app crashes. So you can take my word for it. But the question still remains: How do you know it's not crashing? – greeble31 Mar 06 '19 at 15:37

0 Answers0