DEV Community

Cover image for Easy reoccurring task scheduler in Android
Ali Alp
Ali Alp

Posted on • Edited on

1 1

Easy reoccurring task scheduler in Android

In mobile development often there is a need to run a method couple of times, or on a fixed schedule , like playing an animation or fetching data every some seconds, according to the Android's best practices, it is recommended to use the Handler object, the following snippet can come in handy when you have a lot of reoccurring tasks

import android.os.Bundle;

public interface IReoccurringTask {
    void run(Bundle bundle);
}
Enter fullscreen mode Exit fullscreen mode
import android.os.Bundle;
import android.os.Handler;

public class ReoccurringTaskScheduler {

    private Handler handler = new Handler();
    private Runnable runnable;
    private IReoccurringTask reoccurringTask;

    private ReoccurringTaskScheduler(){}

    public ReoccurringTaskScheduler(IReoccurringTask reoccurringTask) {
        this.reoccurringTask=reoccurringTask;
    }

    public void Schedule(long initDelay, final long interval) {

        runnable = new Runnable() {

            @Override
            public void run() {
                try {
                    reoccurringTask.run(null);
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    handler.postDelayed(this, interval);
                }
            }
        };
        handler.postDelayed(runnable, initDelay);
    }

    public void Schedule(final Bundle bundle, long initDelay, final long interval) {

        runnable = new Runnable() {

            @Override
            public void run() {
                try {
                    reoccurringTask.run(bundle);
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    handler.postDelayed(this, interval);
                }
            }
        };
        handler.postDelayed(runnable, initDelay);
    }

    public void cancel() {
        if (runnable != null)
            handler.removeCallbacks(runnable);
    }
}
Enter fullscreen mode Exit fullscreen mode

and you can define your custom logic like this

private class ReoccurringLogicClass implements IReoccurringTask{

    @Override
    public void run(Bundle bundle) {

        //reoccurring logic
    }


}
Enter fullscreen mode Exit fullscreen mode

and then use it like this

ReoccurringTaskScheduler rTask=
    new ReoccurringTaskScheduler(new ReoccurringLogicClass()).Schedule(1000,2000);
Enter fullscreen mode Exit fullscreen mode

and cancel it like this

rTask.cancel();
Enter fullscreen mode Exit fullscreen mode

Happy coding :)

Sentry mobile image

Mobile Vitals: A first step to Faster Apps

Slow startup times, UI hangs, and frozen frames frustrate users—but they’re also fixable. Mobile Vitals help you measure and understand these performance issues so you can optimize your app’s speed and responsiveness. Learn how to use them to reduce friction and improve user experience.

Read the guide

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