0

I need to start thread from my application which downloads from server JSON file periodically.
I created Service for this purpose and started the thread in onStartCommand as recomended in android literature.
The question is should I really create the service in order to start the thread or I could implement everithing within the main Activity?

Costa Mirkin
  • 947
  • 3
  • 15
  • 39

3 Answers3

4

You can implement everything in the activity. The service you only implement if you need something running whilst the activity is not running.

the two best approaches within the activity are:

AsyncTask http://developer.android.com/reference/android/os/AsyncTask.html

AsyncTaskLoader http://developer.android.com/reference/android/content/AsyncTaskLoader.html

personally I prefer this second one, the loaders.

Budius
  • 39,391
  • 16
  • 102
  • 144
1

You are right you should create service in which just create handler or create any thread or use time task for periodically check json file over server,this is good way i think.

thank you

Bhavdip Sagar
  • 1,951
  • 15
  • 27
1

Since you are downloading the JSON and not using the service for anything else, you can have a IntentService to do this check the Wakeful IntentService for periodically scheduling a download. More information read the documentation from android.

  1. Wakeful IntentService
  2. Intent Service Documentation
  3. android design considerations: AsyncTask vs Service (IntentService?)
Community
  • 1
  • 1
Lalith B
  • 11,843
  • 6
  • 29
  • 47