开发者

Timer problem in android not outputting anything

开发者 https://www.devze.com 2023-02-07 12:51 出处:网络
I have the following code: Timer micTimer = new Timer(); micTimer.schedule(new TimerTask(){ @Override public void run(){

I have the following code:

Timer micTimer = new Timer();
micTimer.schedule(new TimerTask(){
    @Override
    public void run(){
         Log.v("Timer", "Timer");
    }
}, 0, 1000);

Which in theory should every second output a log line Timer Timer, bu开发者_运维百科t does not. I understand its probably a better way to use a Handler and use the postDelay() method but this way if working seems adequate enough for my needs. I don't understand what is wrong with the code.

.


Try this:

class UpdateTimeTask extends TimerTask {
   public void run() {
       Log.v("Timer", "Timer");
   }
}

--

Timer micTimer = new Timer();
micTimer.schedule(new UpdateTimeTask(), 0, 1000);

TimerTask is abstract, so you can't instantiate it directly - you need to extend it.

0

精彩评论

暂无评论...
验证码 换一张
取 消