我可以使用哪些选项来检查 IntentService 是否已在运行?

发布于 2024-10-19 19:51:35 字数 217 浏览 4 评论 0原文

我有一个 AlarmService,它每 15 分钟唤醒一次并触发 IntentService。但是,我想确保如果先前启动的 IS 已经在运行,则 AlarmService 不会尝试启动另一个 IS(IS 正在处理文件,如果第二个版本尝试启动,则会出现奇怪的竞争条件)作用于相同的文件)。

轮询系统以查看我的 IS 实例是否已在运行并跳过 AlarmService cron 的当前迭代的最佳方法是什么?

I have an AlarmService that wakes up every 15 minutes and fires off an IntentService. However, I would like to make sure that if a previously started IS is already running, that the AlarmService doesn't try to start another one (the IS is dealing with files and there would be an odd race condition if a second version tried to act on the same files).

What's the best way to poll the system to see if an instance of my IS is already running and just skip the current iteration of the AlarmService cron?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

執念 2024-10-26 19:51:35

AlarmService 不会尝试启动另一个版本(IS 正在处理文件,如果第二个版本尝试对同一文件执行操作,则会出现奇怪的竞争条件)。

不会有竞争条件。首先,IntentService 只会有一个实例。其次,IntentServiceonHandleIntent() 中一次只会处理一个 Intent

轮询系统以查看我的 IS 实例是否已在运行并跳过 AlarmService cron 的当前迭代的最佳方法是什么?

不要那样做。在您的 IntentService 中,跟踪您上次在数据成员中进行操作的时间。如果在 onHandleIntent() 中,该值为 null,那么这是您一段时间以来第一次经过,因此您可能想继续执行该工作。如果该值不为null,则将其与当前时间进行比较,如果太早,则从onHandleIntent()返回。

that the AlarmService doesn't try to start another one (the IS is dealing with files and there would be an odd race condition if a second version tried to act on the same files).

There will be no race condition. First, there will only ever be one instance of the IntentService. Second, the IntentService will only process one Intent at a time in onHandleIntent().

What's the best way to poll the system to see if an instance of my IS is already running and just skip the current iteration of the AlarmService cron?

Don't do it that way. In your IntentService, track the last time you did work in a data member. If, in onHandleIntent(), that value is null, it's your first pass through in a while, and so you probably want to go ahead and do the work. If the value is not null, compare it to the current time, and if it's too soon, just return from onHandleIntent().

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文