Executing tiny asynchronous tasks quickly in Android under 10 lines
Tags: android, todayilearned, androiddev, coding
Published: 2018-11-19 17:46:46

---
title: Executing tiny asynchronous tasks quickly in Android under 10 lines
published: true
tags: android,today-i-learned,androiddev,coding
canonical_url: https://android.jlelse.eu/executing-tiny-asynchronous-tasks-quickly-in-android-under-10-lines-7ebaa2103e9b
---

#### Without using any frameworks like RxJava or managing Threads

> This article is part of [Today I Learned](https://medium.com/p/65c64e1dcb6) series and was originally posted at my [TIL Github Repository](https://github.com/wajahatkarim3/Today-I-Learned/) and my website at [wajahatkarim.com](http://wajahatkarim.com)

Today, when I was working on a project, so I had a situation where I had to perform a very tiny operation in background in Android. I had lots of ideas such as AsyncTask or Thread or using such powerful framework like RxJava or using new APIs such as JobDispatcher or WorkManager etc. You can read about these options in [this good article](https://android.jlelse.eu/8-ways-to-do-asynchronous-processing-in-android-and-counting-f634dc6fae4e) by [Ali Muzaffar](https://medium.com/u/27c3b88ddc65).

But my major concern was that my requirement was a very simple and little one. I wanted to get address from the location in form of latitude and longitude. And I wanted to do it in background along with showing ProgressBar and stuff. You can check the below GIF image as the result of what I did.

![](https://cdn-images-1.medium.com/max/320/1*eqYVg-vCFgF7ul2QHuWffg.gif)<figcaption>A part of the app I am working on.</figcaption>

I had a simple method in Utilities class which performed Geocoding on LatLng object and returned an address in String or null if there’s not valid address.

![](https://cdn-images-1.medium.com/max/1024/1*RWwY_xabWOzzpPIQe9rkZQ.png)

I had setup GoogleMap.OnCameraIdleListener on a GoogleMap object to get the location of the center marker after dragging the map and called the above getAddressFromLocation() method there to get the address like this.

![](https://cdn-images-1.medium.com/max/1024/1*Pjn5wxm3w3wAykzCRa8Lbw.png)

This was working but this had a small issue. If the address takes time in Geocoding, then the progress bar becomes stuck and screen hands including all the UI. This is because long operation is being on the main thread. And according to Android requirements, it should happen on the Worker or any other thread so that UI and main thread cannot be blocked.

Now, I had to put this line of calling getAddressFromLocation in another Thread or something just to make sure that main thread is not blocked. On a simple google search, I found plenty of ways like [in this article](https://android.jlelse.eu/8-ways-to-do-asynchronous-processing-in-android-and-counting-f634dc6fae4e). I wanted something simpler.

I liked the way of AsyncTask but I didn’t wanted to make another class extending AsyncTask . So, when I dig deeper, I found that there is a static method AysncTask.execute() which performs small operations in Worked thread without creating any extra classes.

So, after adding that my above code became like this:

![](https://cdn-images-1.medium.com/max/1024/1*xBRn1oYUo_T4xsF2LaokkA.png)

As you can see, using this simple AsyncTask.execute()method made this thing very simpler. One more thing to note here is that we cannot perform UI operations like setting text on TextView or hiding ProgressBar in this Worker thread. These can only be performed in Main/UI thread. So, that’s why I have called runOnUiThread() method in the thread.

After compiling and running the app, this was the result.

![](https://cdn-images-1.medium.com/max/320/1*eqYVg-vCFgF7ul2QHuWffg.gif)

**Wajahat Karim** is a graduate from NUST, Islamabad, an experienced mobile developer, an active open source contributor, and co-author of two books [Learning Android Intents](https://www.amazon.com/Learning-Android-Intents-Muhammad-Usama/dp/1783289635) and [Mastering Android Game Development with Unity](https://www.amazon.com/Mastering-Android-Game-Development-Unity/dp/1783550775/). In his spare time, he likes to spend time with his family, do experiments on coding, loves to write about lots of things (mostly on blog and medium) and is passionate contributor to open source. In June 2018, one of his library became #1 on [Github Trending](https://github.com/trending). [His libraries](http://github.com/wajahatkarim3) have about 2000 stars on Github and are being used in various apps by the developers all around the globe. Follow him on Twitter and [Medium](https://medium.com/@wajahatkarim3) to get more updates about his work in Writing, Android and Open Source.

[Wajahat Karim (@WajahatKarim) | Twitter](https://twitter.com/WajahatKarim)

Also, if you have any questions you’d like him to answer, contact him through his website at [wajahatkarim.com](http://wajahatkarim.com) with DEAR WAJAHAT in the subject line.

* * *