Tasker
Tasker
is intended to enable proper and easy use of the UI and the background thread. AsyncTask
is going to be deprecated in API level 30. Android reference suggests to use the standard java.util.concurrent
class as alternative (more here). Tasker
is a wrapper class of java.util.concurrent
to run computation in the background thread and publish the result in the UI thread. The source code is published under MIT License and the license is available here. An article about Tasker
is available here.
Table of Contents
Supported SDK
The latest version is available for,
- Android SKD 14 and higher
Getting Started
An Tasker
object lets you perform operations in the background. When they've finished running, then it allows you to update UI in the main thread.
Installation
For Gradle:
repositories {
...
mavenCentral()
}
dependencies {
...
implementation 'dev.alshakib.tasker:tasker:{lastest_version}'
}
For Maven:
<dependency>
<groupId>dev.alshakib.tasker</groupId>
<artifactId>tasker</artifactId>
<version>{lastest_version}</version>
<type>aar</type>
</dependency>
Usages
For basic usages, you'll need to create a Tasker
object, and pass a Tasker.Task<Result>
object to the executeAsync()
method.
You can create a Tasker.Task<Result>
class by extending the Tasker.Task<Result>
class, and implementing its doInBackground()
method. The code in this method runs in a background thread, so it's the perfect place for you to put your code for a heavy job. The Tasker.Task<Result>
class also has an onPreExecute()
method that runs before doInBackground()
and an onPostExecute()
method that runs afterward.
Tasker.Task
is defined by a generic parameter: Result which is the type of the task result. You can set this to Void if you're not going to use it.
class MyTask extends Tasker.Task<Result> {
@Override
protected void onPreExecute() {
// Code to run before executing the task.
}
@Override
protected Result doInBackground() {
// Code that you want to run in a background thread.
return null;
}
@Override
protected void onPostExecute(Result result) {
// Code that you want to run when the task is complete.
}
}
You can run a Task
by calling the Tasker executeAsync()
method and passing it a Tasker.Task
object.
Tasker tasker = new Tasker();
tasker.executeAsync(new MyTask());
For a practical example, see here.
Contributing
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
Thanks to
- Project Icon is made by Pixel Perfect from www.flaticon.com
License
Copyright © 2020 Al Shakib