2

I have an application that needs updating constantly. I would like to create a job (Service, thread ...?) that execute the polling over all the activities. I need to update some data even if i'm not on the activity that needs to.

I would not create a service because actually I don't need to update tha application along it is closed. What do you suggest? Thanks a.

alinflames
  • 69
  • 10
  • Your question is unclear. What specifically are you trying to accomplish, please elaborate and be specific. also [What have you tried?](http://mattgemmell.com/2008/12/08/what-have-you-tried/) – FoamyGuy Jun 28 '12 at 16:19

1 Answers1

2

I think you are confusing an Activity with the data that it presents to the user. You do not mess with other activities that are not currently on screen.

If I understand correctly, you want to update some data that is shared among your activities whenever any activity is currently visible.

You could create a Service which in turn uses a separate thread to do its polling. Then in each of your activities that you want the polling to be run you call startService in the onResume method of the activity and stopService in the onPause method of the activity.

Also make sure you use returnSTART_STICKY at the end of your service's onStartCommand method.

skynet
  • 9,898
  • 5
  • 43
  • 52
  • A Service is definitely the way to go. Please read http://developer.android.com/guide/components/fundamentals.html – shkschneider Jun 28 '12 at 16:25
  • @Craigy Thanks for the reply, sorry i'ma a beginner. I try to be a little more specific.This is a really simple application: there is a list of tasks that the user have to complete. Every task has a detail page.

    I need to refresh the list also if the user is in the detail page (or other pages).

    You suggest to use a service. Ok question, this server can be a AsyncTask? ... I try... thanks. a.
    – alinflames Jun 28 '12 at 16:40
  • Sure, you can use an `AsyncTask` inside the service. See http://stackoverflow.com/questions/2750664/is-it-possible-to-use-asynctask-in-a-service-class – skynet Jun 28 '12 at 16:43
  • Thank you! I've done... easier than I thought. One last thing, if I call the stopService on the onPause method of the List activity, when I call the Detail activity the service stops... ? And I need it alive... – alinflames Jun 28 '12 at 17:33
  • Yes you will need to call `startService` in the `onResume` method of any activity where you want the polling to happen (and remember to put `stopService` in the `onPause` of that same activity). – skynet Jun 28 '12 at 17:35
  • Sorry for the delay. I implemented in this way: I created a service which instantiates a TimerTask that makes the polling in the onCreate method and which stops the TimerTask in the onDestroy method. Then i put in all the activities the startService and the stopService respectively in the onResume and onPause method... – alinflames Jun 29 '12 at 13:01
  • It wasn't better to have a unique entry point and a unique exit point of the service? – alinflames Jun 29 '12 at 13:08