-1

I want to have a periodically task that should be run every 30 seconds. so I'm using ScheduledThreadPoolExecutor or Timer

 ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(1);
        executor.scheduleWithFixedDelay(new MyTask(), 0, 30000, TimeUnit.MILLISECONDS);

class MyTask implements Runnable {
        @Override
        public void run() {
        }        
}   

Here is Timer

Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                // do something here
            }
        }, 0, 30000);

My question is: is there any differences if I start above code inside Service/IntentService or inside one activity. Those actions will be same or start inside service will better.

Trần Kim Dự
  • 5,872
  • 12
  • 55
  • 107
  • Possible duplicate of [Android timer? How?](http://stackoverflow.com/questions/4597690/android-timer-how) – karan Aug 24 '16 at 05:20

1 Answers1

0

If you want to run this task even when your app is in backgroud then You should use a service or if you want to run in only when your app is in front so you may use it in activity

Umar Ata
  • 4,170
  • 3
  • 23
  • 35