2

I have an app that has a Service that offloads photos that people are taking to the server.

Specifically, users are sent to the device's native camera to take a photo and then the photo is returned via intent to the app from which they "approve" it.

This act of approval saves it out to the file system and the Service comes along every x seconds, notices files awaiting offload and offloads them.

This all works fine.

However... in situations where there is bad connectivity but enough for the HTTP handshake, the app finds itself in a state where even though the offload is happening in a Service, at the point where the user is coming *back to my app from the photo taking (and Android is delivering the 4-8mb photo back to my approval Activity, my app hangs - sometimes long enough to provoke the "do you want to kill or wait" prompt. Eventually (if you wait) it does succeed in making its way back to the app.

I've verified that this is Network related because when the connectivity is strong (or when the app is in airplane mode - so the upload just fails instantly) everything works perfectly smoothly. The *only time this happens is when the offloader in the Service is hampered by a hinky connection.

So, my question is - what can I do about this? Is there some way I can isolate the Service to not have a larger effect on the app? Is my solution to write a partner app that sits on the device and just looks to offload the files (would that even solve the problem?).

HERE is the report I'm getting when the WAIT/KILL prompt is offered to the user. I'm not sure what to make of it.

Yevgeny Simkin
  • 27,946
  • 39
  • 137
  • 236
  • "my app hangs" -- use Traceview and `StrictMode` to determine where you are spending too much time on the main application thread. – CommonsWare Oct 01 '15 at 18:09
  • Hi Commons! I just attached the trace. I can't really tell what it's telling me exactly, though I suspect it's reinforcing my suspicion that this is all being caused by a very slow/bad internet connection. – Yevgeny Simkin Oct 01 '15 at 18:10
  • "I just attached the trace" -- that has nothing to do with Traceview or `StrictMode`. – CommonsWare Oct 01 '15 at 18:14
  • sorry... I didn't mean to imply that it does. I attached that as you were writing your comment and just noted that it's now attached. I'm adding StrictMode now and will report back with what I find :) As always, thanks for your help. – Yevgeny Simkin Oct 01 '15 at 18:21
  • Sorry. The only thing your trace tells me is that temporarily removing the NewRelic bits might be an interesting experiment, to see if they are actually contributing to the problem. – CommonsWare Oct 01 '15 at 18:26
  • @CommonsWare, so, as per Ben's answer, this was indeed my 'bad' in that I didn't realize that Service operations aren't running in their own thread. However, that brings up the question - why are we required to run our networking operations in a threaded way when in the UI thread, but can gum up the same processor space unthreaded in the Service? Is this an oversight or is this a reasoned decision and if so do you know if Google documented their reasoning anywhere? – Yevgeny Simkin Oct 01 '15 at 19:57
  • "Is this an oversight or is this a reasoned decision" -- it's a decision based on the reasons of 10-12 years ago, back when mobile CPUs were in the single-core, 33MHz range and threads were expensive. Android wouldn't fork any more threads than were necessary, and in particular would not automatically fork threads for services or broadcast receivers, relying on developers to do their own thread management. – CommonsWare Oct 01 '15 at 20:01
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/91132/discussion-between-genia-s-and-commonsware). – Yevgeny Simkin Oct 01 '15 at 20:03

1 Answers1

0

The answer turned out to be that Services are actually running on the main Display thread (I was mislead by the fact that when you make an HTTP call in a Service you don't have to run it in a separate thread manually).

http://gmariotti.blogspot.com/2013/03/antipattern-freezing-ui-with-service.html

Yevgeny Simkin
  • 27,946
  • 39
  • 137
  • 236