唤醒锁的替代品? [英] Alternatives to Wakelocks?

查看:56
本文介绍了唤醒锁的替代品?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个需要每隔X分钟将数据发送到Web服务器的应用程序(其中X是可以通过Web服务器响应进行更改的值).

I'm working on an app which needs to send data to a webserver after every x minutes (where x is a value that can be changed through webserver responses).

现在,我正在使用一个后台线程,该线程休眠x分钟,然后通知主线程开始发送数据.

Right now, I'm using a background thread that sleeps for x minutes and then notifies the main thread to start sending data.

但是,由于CPU睡眠,我遇到了这种方法的问题,这似乎会影响线程的睡眠时间.

However, I'm experiencing issues with this method due to CPU-sleeping, which seems to affect the thread sleeping time.

我已经阅读了有关唤醒锁的信息,并认为我可以实现部分唤醒锁...但是,此应用程序的核心功能之一是节省电池,因此总体唤醒锁是不可行的.

I've read about wakelocks and think I could implement a partial wakelock...however, one of the core features of this app is to save battery, so overall wakelocks just aren't feasible.

所以我想知道唤醒锁是否有其他选择可以确保每x分钟后总是在后台运行某种逻辑,理想情况下仅在需要时才唤醒CPU?

So I was wondering if there are any alternatives to wakelocks that can make sure that a certain piece of logic always runs in the background after every x minutes, ideally only waking up the CPU when it is needed?

谢谢.

推荐答案

使用 SheduledThreadPoolExecutor .它可以处理单个任务,定期任务等...

Use SheduledThreadPoolExecutor for this. It can handle single tasks, periodic tasks etc...

这篇关于唤醒锁的替代品?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆