1

I have implemented JobService for my app. The job makes an API call. This has to be done every 4 minutes.

Also the job should start only when the app is being used and should stop when the user exits the app.

I am using setPeriodic to make it run every 4 minutes.

I am planning to do stop/cancel the job in onStop activity lifecycle method. But how do I make sure that the job is stopped/cancelled while the user closes the app?

In onStartJob method, I am returning false once I receive the response for my api call. Will this stop the job? or should I do something else?

NewOne
  • 401
  • 5
  • 19
  • in onDestroy() or onStop() methods of activity just call the jobschedulerservice and cancel all the pending jobs. – Devil10 Mar 20 '19 at 04:20

1 Answers1

0

The minimum period for a job to run using job scheduler is 15 minutes in the standard AOSP. So JobService doesn't look like the right thing to use for your problem. You should look into Bound Services, detach when exiting the application. Your 4 min logic should be implemented manually inside the service.

farhan patel
  • 1,490
  • 2
  • 19
  • 30
  • Thanks for the reply. I am facing this issue: https://stackoverflow.com/questions/55136177/background-execution-in-oreo-android-devices – NewOne Mar 28 '19 at 04:31
  • Can I use handler to schedule every 4 mins and inside that start and end job service(service makes an api call). So on exit of the app, I will cancel the job using the job id. is this possible? thank you – NewOne Mar 28 '19 at 04:32
  • Yes it is one the the possible approaches. – farhan patel Mar 28 '19 at 04:35
  • Thanks. Can you please let me know if job service can fix my issue? https://stackoverflow.com/questions/55136177/background-execution-in-oreo-android-devices – NewOne Mar 28 '19 at 07:50
  • Nope. Job scheduler is meant for background tasks, Communicating between activities can be done via an interface callback – farhan patel Mar 28 '19 at 08:45
  • could you please explain more about this solution? Any article to read? Mine is cordova app. First activity is a WebView. Javascript calls stop after 5 mins of going in background. How to make the javascript calls to run even when the activity is in background? – NewOne Mar 28 '19 at 09:02