简体   繁体   中英

How to stop jobservice when the user exits/closes the app?

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM