android - USB 未连接时 ScheduledExecutorService 停止

标签 android service scheduled-tasks

在我的应用程序中,我有一项服务正在向服务器查询数据。 该服务每 x 分钟查询一次服务器,这是可以配置的。 当我运行应用程序时,连接到 USB 电缆,

timer.schedule(new MyTimerTask(), delayInSec, TimeUnit.SECONDS);

完美运行。但是当设备没有连接到USB(电脑)时,它停止了。

当设备重新连接到 USB 时,它会恢复工作。

如何解决?

谢谢, 埃亚尔。

最佳答案

How it can be solved?

第 1 步:删除现有的 ScheduledExecutorService 实现。

第 2 步:使用 AlarmManager 设置您的计划工作,让工作由 WakefulIntentService 执行,所以设备会醒来做工作,保持清醒足够长的时间来完成工作,然后在完成后回到 sleep 状态(让你的服务在工作之间关闭,所以它不会经常占用用户的内存) .

关于android - USB 未连接时 ScheduledExecutorService 停止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10162532/

相关文章:

android - 相机屏幕方向问题三星盖乐世 S3

android - Gradle 同步失败 : Could not find method compile() for arguments [com. android.support :appcompat-v7:25. 3.1]

android - 来自服务的持久性 android 通知

objective-c - 在后台服务中执行操作的任务

php - 使用 PHP/MySQL 显示一些统计信息的最有效方法

android - 如何使用 "Share image using"共享 Intent 在 android 中共享图像?

android - "Unparseable date: 1302828677828"尝试使用 Gson 反序列化从服务器接收到的毫秒格式日期

java - 如何在 AIX 中将 jar 文件作为服务运行,以便在崩溃时自动重新启动?

service - ILE RPG 使用 CRTSQLRPGI 通过引用进行绑定(bind)

c# - MVC中使用webbackgrounder nuget长时间运行后台任务