0

I have service in which I want to take pictures of the user. I have a timer task

     myTimer = new Timer();
     myTimer.schedule(new TimerTask() {
        @Override
        public void run() {
            TimerMethod();
        }

    }, 0, 3000);

And the timer method

private void TimerMethod() {

    if (na != null)
        zzz.runOnUiThread(Timer_Tick);
}

private Runnable Timer_Tick = new Runnable() {
    public void run() {

        takePhoto(getApplicationContext(), 1);
    }
};

My problem is that if i runOnUiThread the app is running slower.

How can I run in other thread that will not influence the user experience?

Many thanks.

davinci.2405
  • 247
  • 1
  • 4
  • 15

1 Answers1

0

Your TimerTask is running in main thread,this will make app runnning slower.You need a new thread to run method TimerMethod,just like this:

  myTimer.schedule(new TimerTask() {
    @Override
    public void run() {   
            new Thread(new Runnable() {

                @Override
                public void run() {
                    TimerMethod();
                }
            }).start();
       }

   }, 0, 3000);
simonws
  • 65
  • 5
  • Hello, so i should skip zzz.runOnUiThread(Timer_Tick); and make something like this? takePhoto(getApplicationContext(), 1); inste run and remove timerMethod ? – davinci.2405 Aug 06 '15 at 10:08
  • The only principle is that you should run time-consuming method in a new thread,not in main thread.Methods in service such as onCreate(),onStartCommand() are all run in main thread. – simonws Aug 06 '15 at 11:38