Android - perform repititve task in service
I'd like to add a repi开发者_高级运维titive taksk to a Service in my Android app. I've read about Runnable/Handler constructs, and about the Timer.scheduleAtFixedRate(). I'm wondering which one is the best approach.
I'm especially worried about the "scheduleAtFixedRate()" being run multiple times at once if execution takes longer than the interval. Or is that not possible?
How long is the interval? For this purpose, i think is good to use android AlarmManager.
It is for scheduling events on android, you can see a nice example here. And you can choose the method setRepeating instead set for repetive events.
static final long DELAY = 4000;
TimerTask task= new TimerTask (){
public void run(){
//do what you needs.
timer.shedule(this, DELAY);
}
}
Timer timer = new Timer();
timer.shedule(task, 0);
You can try this.
精彩评论