Android app to change wallpaper at regular intervals using Timer

此生再无相见时 提交于 2019-11-29 04:52:11

It looks like you're using the timer wrong. If you want to have it recur, you need to specify an initial delay as the second argument, and an interval as the third. Timer.schedule(timertask, initial delay, interval between recurrences);

Note: I'm talking about your call to myTimer.schedule(object, interval);

Try instead of Timer class ScheduledFuture
This helped for me to resolve all problems with timer tasks
Good luck!

private ScheduledFuture mytimer;

//...

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    ScheduledExecutorService timer = Executors.newScheduledThreadPool(1);
    mytimer = timer.scheduleWithFixedDelay(new TimerTask() {
        @Override
        public void run() {
            //...
        }
    }, 0, interval, TimeUnit.MILLISECONDS);
    return super.onStartCommand(intent, flags, startId);
}

//...

@Override
public void onDestroy() {
    super.onDestroy();
    if (mytimer != null) {
        mytimer.cancel(true);
    }
    //...
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!