Android Loopers and Handlers – Code Tutorial

In our last post we tried to understood the concept of Looper and Handler. Now let us implement these in this tutorial. We will be creating a download manager which download files(we are using delay for this simulation) one by one.


Implementation Details

Basically, I created a DownloadTask class that emulates a download task of a random duration. I did not want to do actual downloads because I didn’t want to eat your data plans, but it’s straightforward to change this into real work. Then, the DownloadThread has an interface that allows to enqueue DownloadTask instances and also to request it to exit gracefully. There is also the DownloadThreadListener interface that allows the download thread to notify some other entity about status updates. In our case, that interface will be implemented by the  DownloadQueueActivity because we want to reflect the download progress in the UI.

Source code:

 

[su_button url=”https://github.com/androidsrc/SimpleLooper” target=”blank” style=”stroked” background=”#51d461" color=”#ffffff” size=”6" center=”yes” radius=”0" icon=”icon: arrow-circle-o-down”]Download Complete Source Code[/su_b[/su_button]p> 

Preview:

[su_yo[su_youtube url=”http://youtu.be/U3aTFNWxV6o”]h2>Lets get started :

1. Create a new project in Eclipse by navigating to File ⇒ New Android ⇒ Application Project and fill required details.

2. Lets create DownloadThreadListener.java interface which will be used to get thread updates.

GuRu

Technology enthusiast. Loves to tinker with things. Always trying to create something wonderful using technology. Loves coding for Android, Raspberry pi, Arduino , Opencv and much more.

You may also like...

1 Response

  1. March 12, 2015

    […] this link for detailed code […]

Leave a Reply

Your email address will not be published. Required fields are marked *