Java – unable to start a timer in a service in Android

I don't know what error happened... I can't start the timer in my service. Follow the code

public class BkgService extends Service{

private Timer ServUpdTimer = new Timer();
private static  long TMR_INTERVAL = 10*60*1000;

public void onCreate() {
    super.onCreate();
    StartServUpdateTask();
    }

private void StartServUpdateTask() {

        if(ServUpdTimer != null)
            ServUpdTimer.cancel();


        ServUpdTimer.scheduleAtFixedRate(new TimerTask() {
            @Override
            public void run() {
            }
        }, 0, TMR_INTERVAL);
    }
}

But when I reach the servupdtimer. Scheduleatfixedrate() line, I get the following exception

I would appreciate it if someone could know something about it

resolvent:

From Javadoc for timer. Cancel():

From Javadoc for timer. Scheduleatfixedrate():

Timer is cancelled immediately: you need to create a new instance:

private void StartServUpdateTask() {

    if(ServUpdTimer != null)
    {
        ServUpdTimer.cancel();
    }
    ServUpdTimer = new Timer();

    ...
}

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>