5

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 Timer object. The Timer object would be invoked at regular intervals and change the wallpaper.

This is the code I am using currently. The wallpaper gets changed only once and not after that. Please let me know what should I do?

public class Wallpaper extends Service {

    Timer mytimer;
    int interval=60000;
    Drawable drawable;
    WallpaperManager wpm;
    int prev=1;

    @Override
    public void onCreate() {
        super.onCreate();
        mytimer=new Timer();
        wpm=WallpaperManager.getInstance(Wallpaper.this);
    }



    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        mytimer.schedule(new TimerTask() {
            @Override
            public void run() {

                if(prev==1){
                    drawable = getResources().getDrawable(R.drawable.two);
                    prev=2;
                }
                else if(prev==2){
                    drawable = getResources().getDrawable(R.drawable.three);
                    prev=3;
                }
                else{
                    drawable = getResources().getDrawable(R.drawable.one);
                    prev=1;
                }


                Bitmap wallpaper=((BitmapDrawable)drawable).getBitmap();

                try {
                    wpm.setBitmap(wallpaper);

                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        }, interval);

        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public IBinder onBind(Intent arg0) {
        return null;
    }
}

Also, do I need to use an AlarmManager or Handler to achieve this ? I am quite new to Android and a bit confused.

ambit
  • 1,099
  • 2
  • 28
  • 43

2 Answers2

7

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

Ditmar Wendt
  • 668
  • 4
  • 15
  • 1
    Set 0 for delay for immediate start. :) – Calvin Mar 16 '12 at 06:27
  • Thanks..this seems to be working now.. There is some other myTimer.schedule() method with only two arguments, so that was not giving me an error. Anyway, I am happy that I don't have to use Handler etc. – ambit Mar 16 '12 at 06:50
  • Yup! Using only two arguments makes a one-shot timer. Sometimes useful! – Ditmar Wendt Mar 16 '12 at 07:36
  • However, what I have found out is that when the device is in sleep mode, the service stops. Hence, I will have to use the AlarmManager eventually – ambit Mar 16 '12 at 11:36
0

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);
    }
    //...
}
Vlad
  • 7,997
  • 3
  • 56
  • 43