-1

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?

Mars
  • 4,197
  • 11
  • 39
  • 63
  • What Android version? From what I hear, versions below 2.0 used to like to kill your services even if you specify that they run forever. The solution here is to use a status bar notification to hold the process open. – Gareth Davidson Nov 08 '10 at 00:51
  • I'd guess that what you mean by fail is that it stops checking once the device fully goes to sleep. Changing your code as CommonsWare recommended will solve that problem. You should probably think more about how often it really needs to be done and look at push options. (http://code.google.com/android/c2dm/) A network connection every 60 sec will drain a battery VERY quickly. – cistearns Nov 08 '10 at 06:50

1 Answers1

5

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.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • thanks I didn't know I could do it with AlarmManager although it's a private app for myself so I'm not concerned about the battery – Mars Nov 08 '10 at 16:44