Android Async, Handler or Timer?

Every 5 seconds, I want to call my web service and get the text (not the images), and then display it in my ImageAdapter. What would be the best way to achieve this?

+8
android handler timer android-asynctask
source share
3 answers

It depends on whether you want to use another thread or not. Do you want the user to be able to interact with the application in the user interface thread when loading images? If so, then I would definitely use AsyncTask with a little ProgressBar ( style="@android:style/Widget.ProgressBar.Small" )

If you don't care about threads, what @inazaruk said.

Edit:. True, these are the most modern applications that retrieve data from a web service, will use AsyncTask with a careful little bootloader in the corner to let the user know its update.

Edit 2: here is an example of using TimerTask to start something every 5 seconds. The key is runOnUiThread() . There may be better ways to link all the elements together , but this accurately displays all the fragments.

 myTimer = new Timer(); myTimer.schedule(new TimerTask() { @Override public void run() { CallWebService(); } }, 0, 1000); } private void CallWebService() { this.runOnUiThread(fetchData); } private Runnable fetchData = new Runnable() { public void run() { asyncTask.execute(); } }; 
+7
source share
 final Handler handler = new Handler(); final Runnable r = new Runnable() { public void run() { callWebservice(); } }; handler.postDelayed(r, 5000); 
+8
source share

You must call asynctask inside the main thread of the application. Asynctask cannot be called in a background thread.

0
source share

All Articles