[英]How to stop jobservice when the user exits/closes the app?
I have implemented JobService for my app. 我已经为我的应用程序实现了JobService。 The job makes an API call.
该作业将进行API调用。 This has to be done every 4 minutes.
必须每4分钟执行一次。
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. 我正在使用setPeriodic使它每4分钟运行一次。
I am planning to do stop/cancel the job in onStop activity lifecycle method. 我打算以onStop活动生命周期方法停止/取消该工作。 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. 在onStartJob方法中,一旦收到api调用的响应,我将返回false。 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. 在标准AOSP中,使用作业计划程序运行作业的最短时间为15分钟。 So
JobService
doesn't look like the right thing to use for your problem. 因此,
JobService
看起来不适合您的问题。 You should look into Bound Services , detach when exiting the application. 您应该查看Bound Services ,在退出应用程序时分离。 Your 4 min logic should be implemented manually inside the service.
您的4分钟逻辑应在服务内部手动实现。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.