You should use a Service
that contains a Handler
to set the delay. The basics of how to set up a Service
are in the android docs. Based on what you're describing, what I would do is control the Service
from within onStartCommand
private static final int LOOP_TIME = 1000 * 20; //20 seconds
private Handler loopHanlder = new Handler();
private Runnable taskToRepeat = new Runnable(){
@Override
public void run(){
//whatever you want to loop
}
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
handler.postDelayed(taskToRepeat, LOOP_TIME);
}
Then all you have to do is start or stop your Service
with an intent from within your Activity
. I'm assuming you want to have the service run constantly once started. If you don't you can just add a flag to the Intent
each time you broadcast and control your service that way, switching actions. But this basic set up should get you going.