6

In my current application for a company Im having a ForegroundService that is running while the workers are on duty (logged in to the app). It is important to keep that foreground service running while users are logged in. To recover from cases where this foreground service gets killed somehow (User task swipe accidently or System kill) ive implemented a periodic JOB with Jobscheduler which reactivates the ForegroundService in case it gets shut down. This technique works well pre Android 8 OREO. START_STICKY and other techniques alone did not do the trick for me.

In Android 8, as soon as the foreground service gets killed, the periodic job gets killed as well. Im getting the notification in logcat that the job is not allowed to run. To my understanding, Jobs schould be able to run even when app is in background or killed. And its working on pre OREO devices the way it should.

To my knowledge, I can fix that by enable the option "autostart" in app settings. But since there is no way to know if employees tunred that on, its not a reliable thing as well.

So my questions: - Why does the Job scheduler stops working as it should in Android 8? - Are there any other reliable techniques I could use to let my ForegroundService recover from shutdowns in ANDROID OREO?

Ive read https://medium.com/exploring-android/exploring-background-execution-limits-on-android-oreo-ab384762a66c but that did not answer my questions

Thank you very much

Al Cabone
  • 413
  • 6
  • 17
  • 1
    I know I'm not directly helping with your question, but your implementation would require a ever-running service (not that great). Maybe you could edit and explain more your needs, I'm sure there is a better way. – shkschneider Aug 23 '18 at 09:57
  • I require gps tracking when a worker is logged in into the app since they deliver goods to customers. Customer wants to know where good is. My first implementation was without a foregroundservice, instead using jobs to send periodic gps coordinates to our backend. But that was less reliable because of XIAMOMI and HUAWAI custom androids. Pain in the ass – Al Cabone Aug 23 '18 at 10:24
  • I know custom ROMs might be a pain, but I feel like your previous implementation was the right one. – shkschneider Aug 23 '18 at 11:40
  • Yes they are :/ . Previous implementation was unreliable because of doze mode and battery optimization. For my usecase this implementation was not sufficient since sometimes even on stock android I only recieved a gps update once every 3 hours etc. due to doze. But require it every 15 mins. – Al Cabone Aug 23 '18 at 11:43

2 Answers2

0

did you try to put

                .setPersisted(true)

Note: Requires the RECEIVE_BOOT_COMPLETED permission.

you can also see this question.

Job Scheduler not running on Android N

Momen Zaqout
  • 1,508
  • 1
  • 16
  • 17
  • Yes I tried that and it didnt work for me unfortunately. Job scheduler wont run on Oreo. But thank you! – Al Cabone Aug 23 '18 at 10:19
0

reading some references:

1- The services in andreo oreo if posses a buggle oreo will kill it for energy loss in 7 is a warning but in 8 applies more strict

2 - The jobs scheduler has a time of 15 minutes and avaces is not executed in the precise time debit that they look for the precise moment to execute the event without that the battery of the cellular one is descaste

I recommend that you use this library that provides evernote that is very good and very specific:

https://github.com/evernote/android-job