简体   繁体   English

当用户退出/关闭应用程序时,如何停止jobservice?

[英]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.

相关问题 如何在从后台删除应用程序时停止 JobService Scheduled? - How to stop JobService Scheduled to when app is removed from background? Android:如何在应用关闭时停止监听器 - Android: how to stop listener when app closes 如何在App关闭Android时停止音频 - How to stop Audio when App closes Android 用户关闭应用程序时如何保持AsyncTask - How to Keep AsyncTask when user closes app 应用关闭时如何停止MediaPlayer的声音? - How to stop the sound of my MediaPlayer when the app closes? 当用户关闭应用程序时停止背景音乐,而不是在用户更改活动时停止 - Stop background music when user closes the app, not when user changes activity 即使用户在 android studio 中关闭应用程序,如何保持登录状态 - How to remain signed in even when the user closes the app in android studio 用户关闭应用程序时如何启用推送通知,用户打开应用程序时如何禁用推送通知 - How to Enable Push notification when user closes App and disable when user open app 应用退出时如何销毁静态字段? - How to destroy static field when app exits? 如何创建在应用程序被杀死/在后台时用户退出地理围栏时触发的BroadcastReceiver? - How to create a BroadcastReceiver that is triggered when a user exits a Geofence whilst the app is killed/in the background?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM