android:未连接到电脑时服务表现不同

发布于 2024-09-30 17:00:07 字数 184 浏览 4 评论 0原文

我相信这是因为某些省电选项或其他原因,但我无法调试它,因为它仅在使用电池时才会失败

我有一项每 60 秒检查一次网页的服务 我在服务中使用 asyncTask 来执行此操作 我将其设置为 Thread.thisThread.sleep(60000);在检查之前

我做错了什么吗?睡眠功能会导致服务器被android停止吗?

I believe it's because of some power saving option or whatever but I cant debug it since it only fails when it's on battery

I have a service that checks on a webpage every 60 seconds
I use an asyncTask in the service to do this
and I make it Thread.thisThread.sleep(60000); before checking

am I doing something wrong? could the sleep function cause the server to be stopped by android?

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

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

发布评论

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

评论(1

情定在深秋 2024-10-07 17:00:07

我有一个服务每 60 秒检查一次网页,我在服务中使用 asyncTask 来执行此操作,并将其设置为 Thread.thisThread.sleep(60000);检查前

请不要这样做。

首先,使时间段可配置,包括“永远不要这样做”选项。当开发人员编写主要目的似乎是消耗大量电池寿命的应用程序时,用户确实不喜欢它。保持设备处于唤醒状态并每分钟轮询一次 Web 服务器将消耗大量电池寿命。正是这样的行为导致用户跑向他们能找到的每一个任务杀手。

其次,特别是对于超过一分钟左右的时间段,请使用 AlarmManager 和 [WakefulIntentService][1]。安排 AlarmManager 在用户选择的时间段调用您的应用程序(最好通过 setInexactRepeating())。让 WakefulIntentService 轮询您的网页。如果您遵循记录的 WakefulIntentService 配方,设备将保持清醒状态足够长的时间,以便您获取数据,然后重新进入睡眠状态。您的服务不会一直保留在内存中。您获得了功能,用户获得了更好的设备性能。

I have a service that checks on a webpage every 60 seconds I use an asyncTask in the service to do this and I make it Thread.thisThread.sleep(60000); before checking

Please don't do that.

First, make the period configurable, including a "don't do this, ever" option. Users really do not like it when developers write apps whose primary purpose appears to be to use up a ton of battery life. Keeping the device awake and polling a Web server every minute is going to use up a ton of battery life. It is behavior like this that is causing users to run to every task killer they can find.

Second, particularly for periods greater than a minute or so, please use AlarmManager and a [WakefulIntentService][1]. Schedule the AlarmManager to invoke your application at the user-chosen period (ideally via setInexactRepeating()). Have the WakefulIntentService poll your Web page. If you follow the documented WakefulIntentService recipe, the device will stay awake long enough for you to get your data, then will fall back asleep. Your service will not remain in memory all of the time. You get your functionality, and the user gets better device performance.

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