Android Cron Service

by GarciaPL on Thursday 12 November 2015

I would like to publish a very useful snipper of code to create cron service in your Android app. Of course you might use some libraries created by other developers. A lot of them might be found on site called Android Arsenal [1] which also I would like to recommend for developers who are looking for libraries.

So, First of all you need to create Handler [2] object which allows you to send and process Runnable objects. This object has a lot of methods which might help you to define expected behavior of your code. This time I will use method postDelayed which allows to execute some Runnable with delay.

private Handler handler = new Handler();

And then we can define Runnable with task in it which should be executed with given delay


Runnable networkReceiverTask = new Runnable() {
        @Override
        public void run() {
            //execute here method with given delay
            handler.postDelayed(networkReceiverTask, 15000);
        }
    };


Reference:
[1] Android Arsenal Job Schedulers
[2] Android Handler