0

I have application which for every 10 seconds do some request to server (http client). I read a lot about application life cycle. My application has service with foreground flag and it's work well (application work all time) when android is "active". I don't have phone with real android, so I am testing on emulator, but my friend testing it on smartphone and he notice that when he leave his phone, request are post for 10, 30 minutes, even hour. If he turn on screen, then request time is back to 10 seconds (he have access to server so he see logs). Is this known behavior? Because he installed gmail notifier from google, and this same problem (big delay). Any solution for this? My service have timer task (so request is sent in async task)

Regards

Mustafa Özçetin
  • 1,893
  • 1
  • 14
  • 16
Dibo
  • 1,159
  • 17
  • 34

1 Answers1

2

First of all, if you're polling every 10 seconds, that's going to drain a lot of battery and network bandwidth.

I recommend using a lower frequency or server push.

For the polling issue, how have you implemented the polling? Do you use timers? If so, what options do you pass in? Or do you use a thread that sleeps for 10 seconds?

Depending on the version, Android may turn off all processes, or delay network requests to run every 30 minutes to preserver battery power and bandwidth. (Starting up the network components drain a lot of battery than keeping them running. So If your app turns ON network, do a poll, then simply turn it off, Android may schedule it to align with all other requests on the system.)

Update

You might have to schedule a 'WakeLock' so Android knows when to wake up for your service. I think, by default, android doesn't wake up for timer requests that are scheduled very frequently and it schedules them as I explained. WakeLocks on the other hand can force Android to wake up.

See this question and WakeLock Documentation Make sure you pass the correct parameters, so you don't turn the screen ON. (Would be really annoying.)

Update

I still recommend using server push for this, which will save battery and bandwidth while keeping the updates real time.

halfer
  • 19,824
  • 17
  • 99
  • 186
Madushan
  • 6,977
  • 31
  • 79
  • Service start this timer: @Override public int onStartCommand(Intent intent, int flags, int startId) { timerMain = new Timer(true); timerMain.schedule(new mainTask(), 1000, 9000); startStatusNotification(); // We want this service to continue running until it is explicitly // stopped, so return sticky. return START_STICKY; } In mainTask() method httpclient request is called. I know that it could drain battery, but this application is kind of "I want this event immediately", user know it and known risk. Why android thinks for user? – Dibo Aug 07 '12 at 23:02
  • I haven't found info about 30 minutes, but generally you are right. There are three radio states: full power, low and idle and switching between them with different periods. Full description how android works with netowork can be found [here](http://developer.android.com/training/efficient-downloads/efficient-network-access.html) – marwinXXII Aug 07 '12 at 23:07
  • You might have to schedule a 'WakeLock' so android knows when to wake up for your service. I think, by default, android doesn't wake up for timer requests that are scheduled very frequently and it schedules them as I explained. WakeLocks on the other hand can force android to wake up. [I updated my answer.] – Madushan Aug 07 '12 at 23:35
  • 1
    Users won't appreciate your app pinging the network that is for sure. Consider using tools like traceview http://developer.android.com/tools/help/traceview.html and ARO https://developer.att.com/developer/legalAgreementPage.jsp?passedItemId=9700312 to make sure you are using the network efficiently. There are also some articles that talk about this topic too here https://developer.att.com/developer/forward.jsp?passedItemId=7200042 – Rod Burns Aug 09 '12 at 13:55