android:未连接到电脑时服务表现不同
我相信这是因为某些省电选项或其他原因,但我无法调试它,因为它仅在使用电池时才会失败
我有一项每 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
请不要这样做。
首先,使时间段可配置,包括“永远不要这样做”选项。当开发人员编写主要目的似乎是消耗大量电池寿命的应用程序时,用户确实不喜欢它。保持设备处于唤醒状态并每分钟轮询一次 Web 服务器将消耗大量电池寿命。正是这样的行为导致用户跑向他们能找到的每一个任务杀手。
其次,特别是对于超过一分钟左右的时间段,请使用
AlarmManager
和 [WakefulIntentService
][1]。安排AlarmManager
在用户选择的时间段调用您的应用程序(最好通过setInexactRepeating()
)。让WakefulIntentService
轮询您的网页。如果您遵循记录的WakefulIntentService
配方,设备将保持清醒状态足够长的时间,以便您获取数据,然后重新进入睡眠状态。您的服务不会一直保留在内存中。您获得了功能,用户获得了更好的设备性能。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 theAlarmManager
to invoke your application at the user-chosen period (ideally viasetInexactRepeating()
). Have theWakefulIntentService
poll your Web page. If you follow the documentedWakefulIntentService
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.