1

I am developing an android app which fetches/uploads data from/to the web service every n minutes. This upload/download is only done when the app is running. But this might change in future.

I dont update the UI when the new data is downloaded. The UI is only updated if the user is on the current screen(app have multiple activities)

My question is what is the best approach to this problem. I dont think service is the right approach as it sounds like an overkill(in the present scenario). AlarmManager could be an option. Running threads inside a service be an option ..something like this .

Any pointers/suggestions would be great. Thanks

Community
  • 1
  • 1
jsp
  • 2,546
  • 5
  • 36
  • 63

4 Answers4

5

I am using AsyncTask in my activity to ask .net web service some information and it works and easy to use.

AsyncTask enables proper and easy use of the UI thread. This class allows to perform background operations and publish results on the UI thread without having to manipulate threads and/or handlers.

bob
  • 2,674
  • 1
  • 29
  • 46
  • i tried doing the same initially. I call a thread to connect to my webservice which fetches/uploads data. The onPostExecute gets executed even before all the data is downloaded.. How did you solve that issue or any suggestions on that?? – jsp May 05 '11 at 14:22
  • 1
    @Anonymous In doInBackground(), I execute http get request, once got response(HTTP 200 OK with XML), then onPostExecute() will be called and I use response data parsed from XML. I am doing some authorisation and returning data is small. – bob May 05 '11 at 21:09
  • Why did you not use something like ksoap rather use HttpClient – jsp May 05 '11 at 21:47
  • I started with ksoap then wanted https which I didn't know how to make it work on ksoap.. – bob May 05 '11 at 22:22
1

Well, in this case, since the app would already be running during the time, either would work great, but a service can be called from anywhere within the application so this is where I would use the service over the thread.

If you want to create the thread to only be used in lets say Main.java, then thread would work fine, these are the only things that I can see really making ANY difference at all, they're really pretty close, and in this case neither gives a distinct "correct" answer, but I would choose Service

Samuel
  • 4,337
  • 3
  • 29
  • 35
0

Just broadcast a message after your upload/download is done, and then have a receiver start the service and then have that service stop itself. And you are done.

This should be done if you dont plan on polling the server for new information or anything. Primarily this kind of approach would be for onetime update, interpret, finish. And wait until the next update. Which primarily for most cases is streaming.. but depends on what you are getting.

JoxTraex
  • 13,423
  • 6
  • 32
  • 45
0

I think all approaches you noted would work ok. Personally I'd go with this:

  1. Use AlarmManager to wake download service. Start it when Activity is shown, stop it when activity hidden.

  2. Download service should be short lived: start it to do the upload/download and then shut it down.

  3. If download service does get some new data, it sends a Broadcast which Activity listens to.

Peter Knego
  • 79,991
  • 11
  • 123
  • 154