views:

148

answers:

3

If I want to upload a text file containing some game stats to a web server for testing purpose, which approach is the best? Do I write a service along with my application and then upload the log file once it reaches a specific size? Or do I embed the logic into my application and then do it during idle times?

+1  A: 

I would use a Handler to peridocially initiate the upload. The perform the upload from a AsyncTask.

Nic Strong
Just a quick question, if the Handler resides inside the activity, would that block the UI?
Legend
No as the Handler is not blocking it asynchonously delivers messages to your activity thread. Here is a simple example http://almondmendoza.com/2009/01/05/using-handler-in-android/
Nic Strong
+1  A: 

If you want to trigger the upload periodically as part of an Activity (i.e in response to a user action), then using an AsyncTask is probably the easiest way to do it. You'd only need to use a Service if you want the upload to happen in the background without being initiated by the user or attached to a UI element.

Erich Douglass
+1  A: 

Don't use a Service because you will be using network connectivity and the phone's resources when the user doesn't expect it.

Do it the lazy way and under the bonnet while the app is running using the AsyncTask. If your file never grows above 100k it should be quick enough to send it through. If you are thinking about uploading bigger files >500k then you should probably consider chunking the file in small bit as you need to bear in mind that cell connectivity is flakey.

ruibm