0

I work on application which constantly sends some data acquired from device to server API. And I have a service working all the time in background and collecting data and then a network thread in this service which is responsible for establishing connection with server and sending data.

The problem I have is that although I acquire Wake and WiFi locks on service startup, at some point when device is on 3g network with poor connection it seems that socket write method just hangs: I see this from logs I output after each operation

Network thread is very simple and may be described as

while(true) {
     if(!connectedToServer) {
         connectToServer();
     }
     acquireData();
     sendData();
}

And sendData is very simple - it writes data to socket output stream. From what I see in logs it seems that OutputStream.write() call just blocks for indefinite time on poor connection.

Did anyone experience similar problems?

Thanks,

Anton
  • 4,395
  • 7
  • 32
  • 46
  • Service can drop on network or wi-fi connections, especially if the person using the phone is moving. How often are you actually sending data? If possible, use a repeating alarm to wake trigger the service. The cycle would be: trigger service, service connects, does it's data thing, and disconnects. It will use network or wi-fi automatically at connection time. The timer cycle should be measured in minutes as opposed to seconds. Also, connection failures would be recoverable by catching up on the next connection. – Howard Hodson Sep 24 '12 at 21:40
  • Forgot to mention that alarms are lost at restart or power down. You will need a broadcast receiver that triggers an boot to reset your trigger alarm. – Howard Hodson Sep 24 '12 at 21:43
  • I'm actually streaming data constantly, so I think that alarms don't suite my task. – Anton Sep 25 '12 at 10:12
  • You are going to have unexpected dropouts. You'll need some logic to recover the connection when it does. People are going to walk into buildings that have metal walls or drive into 'quiet' areas. I listen to streaming radio at times and it will 'rebuffer' two or three times in an hour. You may be interested in question http://stackoverflow.com/questions/8849675/getting-response-code-from-httpurlconnection-in-android – Howard Hodson Sep 26 '12 at 15:14
  • You should consider how your application is sending the data too, and ensure that you are not draining the phone battery more than you need. Take a look at some of the articles here that talk about writing efficient network apps http://developer.att.com/developer/forward.jsp?passedItemId=7200042 and also consider using the ARO tool to see how well your app is performing and how you could improve it. http://developer.att.com/aro – Rod Burns Oct 01 '12 at 14:05
  • see this thread also http://stackoverflow.com/questions/19148765/android-reconnect-to-wi-fi-after-entering-coverage-area-while-screen-turned-of/19968708#19968708 – Mr_and_Mrs_D Nov 15 '13 at 01:01

0 Answers0