Task Runner is an Android library that provides a solution for running AsyncTask without using AsyncTask that has been deprecated.
Add it in your root build.gradle at the end of repositories:
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
Add the dependency
dependencies {
implementation 'com.github.paz-lavi:TaskRunner:1.0.0'
}
1. Create a class that extend BaseTask<R>
public class MyTask extends BaseTask<Long> {
Context context;
public MyTask(Context context) {
this.context = context;
}
@Override
public void onPreExecute() {
super.onPreExecute();
}
@Override
public Long call() throws Exception {
long res = 1;
for (int i = 1; i < 999999999; i++) {
res += i;
}
return res;
}
@Override
public void onPostExecute(Long result) {
super.onPostExecute(result);
Toast.makeText(context, String.valueOf(result), Toast.LENGTH_SHORT).show();
}
}
2. Create an instanse of TaskRunner<R>
TaskRunner<Long> taskRunner = new TaskRunner<>();
3. Execute the task
taskRunner.executeAsync(new MyTask(this));
1. Create an instanse of TaskRunner<R>
TaskRunner<Long> taskRunner = new TaskRunner<>();
2. Execute new RunnerCallback<R>
taskRunner.executeAsync(new RunnerCallback<Long>() {
@Override
public void onPreExecute() {
Log.d(TAG, "onPreExecute: ");
}
@Override
public Long call() throws Exception {
long res = 1;
for (int i = 1; i < 999999999; i++) {
res += i;
}
return res;
}
@Override
public void onPostExecute(Long result) {
Log.d(TAG, "onPostExecute: result = " + result);
}
});
Copyright 2020 Paz Lavi
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://github.com/paz-lavi/TaskRunner/blob/master/LICENSE
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.