Android app to change wallpaper at regular intervals using Timer

前端 未结 2 1269
青春惊慌失措
青春惊慌失措 2020-12-17 03:15

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 usin

相关标签:
2条回答
  • 2020-12-17 03:32

    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);
        }
        //...
    }
    
    0 讨论(0)
  • 2020-12-17 03:46

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

    0 讨论(0)
提交回复
热议问题