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?

- 947
- 3
- 15
- 39
-
http://stackoverflow.com/questions/6343166/android-os-networkonmainthreadexception Look here. – Ajay S Dec 28 '12 at 10:46
-
It's not UI thread - I start new thread from the activity – Costa Mirkin Dec 28 '12 at 10:52
3 Answers
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.

- 39,391
- 16
- 102
- 144
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

- 1,951
- 15
- 27
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.