Skip to content Skip to sidebar Skip to footer

Android App To Change Wallpaper At Regular Intervals Using Timer

I wish to create an app, which would change the wallpaper of the Android device at fixed intervals, say every hour or so. Currently in my code, I start a service and am using a Tim

Solution 1:

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);

Solution 2:

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

private ScheduledFuture mytimer;

//...@OverridepublicintonStartCommand(Intent intent, int flags, int startId) {
    ScheduledExecutorServicetimer= Executors.newScheduledThreadPool(1);
    mytimer = timer.scheduleWithFixedDelay(newTimerTask() {
        @Overridepublicvoidrun() {
            //...
        }
    }, 0, interval, TimeUnit.MILLISECONDS);
    returnsuper.onStartCommand(intent, flags, startId);
}

//...@OverridepublicvoidonDestroy() {
    super.onDestroy();
    if (mytimer != null) {
        mytimer.cancel(true);
    }
    //...
}

Post a Comment for "Android App To Change Wallpaper At Regular Intervals Using Timer"