Search code examples
androidperformancehttplow-bandwidth

Is there a way to limit the resources allocated by a Service?


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.


Solution

  • 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