it is logical question so I don't have code. My question is: How may I run background task that make Network work (waiting for messages from the server) and in the same time updates the UI when Message arrive!! the Paradox "Android prevent access to UI from another nonUI thread", and "prevent network accesss from UI thread!!!" Important: I want to run my method all the time that the application run, and scan network buffers and when I get message I want to update the UI and messages List...
-
use a `handler` and post to UI thread. simple example: http://crodrigues.com/updating-the-ui-from-a-background-thread-on-android/ – Amulya Khare Dec 02 '13 at 09:43
2 Answers
That sounds like a perfect description for a Service. You can use IntentService
or build your own Service
(be careful, the last one doesn't start in a new thread by default).
There are also many examples out there how to update the UI from a Service (i.e. using Notifications or Broadcast).
Android provides built in class to perform network operation. For this purpose you can use AsyncTask
class.
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.
You can get details from here Android AsyncTask
But if you want to check message continuously then Service is the best solution as @AlexS explained it perfectly.

- 21,567
- 19
- 47
- 70
-
`AsnycTask` is not meant for continually running in backgroud. See http://stackoverflow.com/questions/6957775/android-asynctask-vs-service – AlexS Dec 02 '13 at 09:53