0

The idea is for my runnable to run every minute.

Instead, it runs in roughly about 20 seconds and I have no idea why.

Below is the code:

    final Handler handler = new Handler();
    Runnable runnable = new Runnable() {

        @Override
        public void run() {
            try{
                //Post from Queue & update post
                if (NetworkUtils.isConnected()) {

                    //post from queue
                    try {
                        postHelper.postFromQueue();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }

                    //Update posts
                    postHelper.updateSolicitations();
                }
            }
            catch (Exception e) {
                // TODO: handle exception
            }
            finally{
                //also call the same runnable to call it at regular interval
                handler.postDelayed(this, 60000);
            }
        }
    };

I don't know if it's relevant but it's onCreate method of MainActivity.

Rosenberg
  • 2,424
  • 5
  • 33
  • 56

3 Answers3

0

Maybe you consider using ScheduledExecutorService

public static void main(String[] args) {
    ScheduledExecutorService execService
                    =   Executors.newScheduledThreadPool(5);
    execService.scheduleAtFixedRate(()->{
        //The repetitive task, say to update Database
        System.out.println("hi there at: "+ new java.util.Date());
    }, Delay, Rate, TimeUnit.MILLISECONDS );//TimeUnit.MILLISECONDS is time unit
}
dali
  • 46
  • 1
  • 7
  • I replace TimeUnit.MILISECONDS with the amount of time I need? Is it async? – Rosenberg May 31 '17 at 13:50
  • in android we are using Handler. – alex May 31 '17 at 13:52
  • @Rosenberg You replace Delay with time you want to delay before this task runs, Rate is the time for repeat ex: execService.scheduleAtFixedRate(()->{ //The repetitive task, say to update Database }, 0, 6000, TimeUnit.MILLISECONDS ); this will repeat every 6 seconds – dali Jun 01 '17 at 08:23
0

Use a scheduled where you define a quartz cronjob that is then triggered whenever you defined it.

you can do something like every minute or second or every day at 3 o'clock

Simple Quartz/Cron job setup

Tim
  • 287
  • 1
  • 13
-4
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
    @Override
    public void run() {                                    

 try{
                //Post from Queue & update post
                if (NetworkUtils.isConnected()) {

                    //post from queue
                    try {
                        postHelper.postFromQueue();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }

                    //Update posts
                    postHelper.updateSolicitations();
                }
            }
            catch (Exception e) {
                // TODO: handle exception
            }



    }
}, 60000);
PRATEEK BHARDWAJ
  • 2,364
  • 2
  • 21
  • 35