0

I have a timer in my TimerService with a task. Every 5 seconds it has to call another Service.

@Override
    public void onCreate()
        {

            super.onCreate();
            Log.i("ServiceDemo01", "Service Starts");
            TimerTask task = new TimerTask()
                {
                    @Override
                    public void run()
                        {

                            startService(new Intent(this, CameraService.class));

                            Log.i("ServiceDemo01", "Picture Taken");
                        }
                };

            timer = new Timer();
            timer.schedule(task, 0, 5000);
        }

But i can't do it in a timer, how can i solve?

startService(new Intent(this, CameraService.class));
paolo2988
  • 857
  • 3
  • 15
  • 31

1 Answers1

4

Yes, but to get it to work you must set a proper context:

startService(new Intent(TimerService.this, CameraService.class));
znat
  • 13,144
  • 17
  • 71
  • 106
  • I've had similar challenges. To be clear, the problem was that in the original code "this" referred to the anonymous TimerTask and not the TimerService. I generally have my anonymous classes contain a simple call to a method of the containing class to avoid issues with scope. – William T. Mallard Jun 05 '15 at 16:53