-1

UPDATE : The code is working only that I dint init the textview, but this question is answered so I cant remove it either. So I will leave this question as it is for anyone trying to implement a Timertask with handler that makes use of Looper.getMainLooper that directly attaches it to the UI THREAD.

OLD QUERY :Hello guys I am trying to implement a timer that runs a task which has a handler. I am using it to update the UI every second. This is what I am implementing:

 private void setRepeatingAsyncTask() {

        handler = new Handler(Looper.getMainLooper());
        timer = new Timer();

        TimerTask task = new TimerTask() {
            @Override
            public void run() {
                handler.post(new Runnable() {
                    public void run() {
                        try {
                               i++;
                            tview.setText(String.valueOf(i));
                        } catch (Exception e) {
                            // error, do something
                        }
                    }
                });
            }
        };

        timer.schedule(task, 0, 1000);  // interval of one minute

    }

when I make setRepeatingAsyncTask() on create or somewhere else like button clicklistner, etc either the timer or the handler is not starting. Please help new to android!

  • 1000 is 1 sec , not 1 minute – W4R10CK Jan 26 '17 at 06:05
  • 2
    @W4R10CK nope 1000 is 1 sec it is in ms PS: don't mind the comment –  Jan 26 '17 at 06:06
  • I'm saying the same. but the error placing of `,` – W4R10CK Jan 26 '17 at 06:07
  • @W4R10CK the middle 0 is post delay which I don't want, and 1000 reps the time taken to execute the task –  Jan 26 '17 at 06:09
  • 2
    See where it says `error, do something`? Have you tried doing something there? Ignoring `Exception` is usually a bad idea. – Mike M. Jan 26 '17 at 06:13
  • 1
    @MikeM. damn was thumping my head all over the desk because I din't init the textview ..cant believe lost 2 hrs on that, lesson bad idea to ignore exceptions :( –  Jan 26 '17 at 06:18

1 Answers1

0

I used Handler to process the task every 1 sec, Using just Handler:

    final Handler handler = new Handler();
    handler.postDelayed(new Runnable() {
        public void run() {

            //some task

            handler.postDelayed(this, 1000); //looping is every 1 secs
        }
    }, 0); //initial delay of 0
W4R10CK
  • 5,502
  • 2
  • 19
  • 30
  • I have tried doing it like your way but there is a slight delay in updating the UI which I dont want. Also there is this new thing Looper that I am trying out –  Jan 26 '17 at 06:13
  • Maybe the delay is because your UI is large or the loop is very fast. – W4R10CK Jan 26 '17 at 06:14
  • 2
    got my code running was just a simple mistake, should have atleast notified a log for my exception regrets that's all I can say, bdw your is also useful as its straight forward for doing simple things. thanks for your time :) –  Jan 26 '17 at 06:21