简体   繁体   English

Android:未连接到PC时服务表现不同

[英]android: service behaved differnat when not connected to pc

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); 我有一个服务,每60秒检查一次网页,我在服务中使用asyncTask来执行此操作,并将其设置为Thread.thisThread.sleep(60000); before checking 在检查之前

am I doing something wrong? 难道我做错了什么? could the sleep function cause the server to be stopped by android? 睡眠功能会导致服务器被android停止吗?

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); 我有一个服务,每60秒检查一次网页,我在服务中使用asyncTask来执行此操作,并将其设置为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. 使设备保持唤醒状态并每分钟轮询一次Web服务器将消耗大量的电池寿命。 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]. 其次,尤其是对于大于一分钟的时间段,请使用AlarmManager和[ WakefulIntentService ] [1]。 Schedule the AlarmManager to invoke your application at the user-chosen period (ideally via setInexactRepeating() ). 安排AlarmManager在用户选择的时间段(理想情况下通过setInexactRepeating() )调用您的应用程序。 Have the WakefulIntentService poll your Web page. WakefulIntentService轮询您的网页。 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. 如果您遵循记录的WakefulIntentService食谱,则该设备将保持唤醒状态足够长的时间以使您可以获取数据,然后将重新进入睡眠状态。 Your service will not remain in memory all of the time. 您的服务不会一直保留在内存中。 You get your functionality, and the user gets better device performance. 您获得了功能,用户得到了更好的设备性能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM