I am new to android. I am developing an application in which a specific piece of code runs every 5 seconds in the background. For this, I use a timer service with a timer. For some time it works fine, but after some indefinite time my service works, but the timer task automatically stops in android. Here is my code, please help. Thanks in advance.
public void onStart(Intent intent, int startid) { //this is the code for my onStart in service class int delay = 1000; // delay for 1 sec. final int period = 5000; // repeat 5 sec. timer = new Timer(); timer.schedule(new TimerTask() { @Override public void run() { executeCode(); }, delay, period); };
source share