0

I have my application that uses JobScheduler to check is it alive or not. As far as it must work in cases:

  • User removed app from recent apps
  • Smartphone was rebooted or battery was exhausted

Anyway application should launch as soon as it is possible.

I used JobScheduler that worked perfect. But since under sdk26 Android doesn't launch my app if it was removed from recents.

What is the solution now? How can I setup JobScheduler to force it to launch my JobService?

Yura
  • 969
  • 14
  • 33

1 Answers1

0

When you schedule your periodic job, use setPersisted(true). This will keep the job scheduled even after a reboot. Note: Requires the RECEIVE_BOOT_COMPLETED permission. After your app is installed, your user must open it one time before the scheduled job stays persistent across reboots.

JobInfo jobInfo = new JobInfo.Builder(
                        JOB_ID, new ComponentName(context, MyJobService.class))
                        .setPeriodic(30 * 60 * 1000)
                        .setPersisted(true)
                        .setRequiresBatteryNotLow(true)
                        .build(); 
Steve Miskovetz
  • 2,360
  • 13
  • 29
  • Did you try how it works under SDK 26? The reason I asked the question is that it doesn't start the job if app is closed (crashed for example) and as I read after that it's Google politics not to start jobs for if service doesn't have foreground app. – Yura Nov 08 '17 at 07:38
  • Yes, I have been using Job Scheduler targeting SDK 26 for a few months now. When I set my job to be persisted, it is automatically scheduled upon boot with my app coming up. I can't speak for my apps when they crash, but when they are swiped away, the job stays scheduled though the service is sticky and comes back up since there are content observers that need to stay observant too. My apps are also system apps so they may behave differently. If I get a chance, I'll try to experiment some more soon outside of the system app sphere. – Steve Miskovetz Nov 09 '17 at 05:50
  • Sorry it has taken me a while to perform some more testing, but now that I have, I haven't seen the issues you have seen in Oreo. If I schedule a job service either on boot completed or within onCreate() of the launcher activity (as a non-system app this time using a Nexus 6P Nov2017 Oreo user build) and setPersisted(true) is set, it will stay scheduled across both: reboots and along with when the app is swiped away from recent apps. My job has the minimum interval of 15 minutes. Also make sure you are meeting your defined contstraints. What are you executing in your JobService? – Steve Miskovetz Nov 22 '17 at 00:13