Android: Uploading a text file periodically?
If I want to upload a text file containing some game stats to a web server for testing purpose, which approach is t开发者_如何学Pythonhe 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?
I would use a Handler to peridocially initiate the upload. The perform the upload from a AsyncTask.
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.
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.
精彩评论