Giter Site home page Giter Site logo

androidtasks's Introduction

Android tasks

This is a small library to create a custom pool thread for Android. It uses annotations to be able to run the code on the UI thread. It also allows the developer to add dependency between tasks, for example if the task A need to wait for the task B, it can be configured in a way that A will be executed once B is finished.

Create a task and execute it

The first thing that we need to do is to create the task:

    private class CustomTask extends BaseTask{

        public CustomTask() {
            super();
        }

        @Override
        public void onFinish() {
            //Do something to finalize the task
        }

        @Override
        public TaskResult runTask() {
            //Do the main stuff
            return null;
        }
    }

Once we have it ready we only need to call TaskExecutor to run the task:

CustomTask customTask = new CustomTask();
TaskExecutor.getInstance().addTask(customTask);

Run the task in the UI thread

To do that we only need to use the @OnUiThread annotation in the method that we want to run on the main loop (onFinish() and/or runTask()):

    private class CustomTask extends BaseTask{

        public CustomTask() {
            super();
        }

        @Override
        @OnUiThread
        public void onFinish() {
            Toast.makeText(TaskWithUiThreadAccessActivity.this, "Task finished", Toast.LENGTH_SHORT).show();
        }

        @Override
        @OnUiThread
        public TaskResult runTask() {
            Toast.makeText(TaskWithUiThreadAccessActivity.this, "Doing something", Toast.LENGTH_SHORT).show();
            return null;
        }
    }

Tasks with dependencies

It is possible to define a task to be executed only when an other task is finished. For instance if we have the task A that depends on the task B we can use the method setTaskIdToWait to force the task A to wait B to finish

CustomTask taskA = new CustomTask(taskId++);
CustomTask taskB = new CustomTask(taskId++);
				
taskA.setTaskIdToWait(taskB.getTaskId());
			
TaskExecutor.getInstance().addTask(taskA);
TaskExecutor.getInstance().addTask(taskB);

androidtasks's People

Contributors

joanpuigsanz avatar

Watchers

James Cloos avatar Sean Pan avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.