DEV Community

Cover image for How to use Anko Library to run background task in Android
Nwokocha wisdom maduabuchi
Nwokocha wisdom maduabuchi

Posted on • Edited on

1 3

How to use Anko Library to run background task in Android

Alt Text

Today I will tell you how easy it is to use Anko Library to run background tasks in android.

There are so many libraries(Rxjava, Loader, AsynTask and more) that can help you do the job but most are very complex and make it tasky to achieve a simple goal of getting out of the main thread.

Coroutines came to the rescue and made everything pretty simple, but Anko team wanted developers to have more time for there logic than learning much on how to work with Coroutines.

Anko takes advantage of Kotlin power to offer an easy and light solution to free the main thread from long-running tasks.

Below are the steps to run a background task with Anko Library

Add the dependency to your project

The first thing is to include the dependency.

As Anko does many things, the size of the library got out of hand. So there came a time when they decided to break it.

For this example, you only need to add the following import:

implementation 'org.jetbrains.anko:anko-common:0.9'

Run the task in a background thread

doAsync {
    var result = runLongTask()
    uiThread {
        toast(result)
    }
}

The code above will run your background task easily.

Sentry blog image

The countdown to March 31 is on.

Make the switch from app center suck less with Sentry.

Read more

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay