I need to implement a service which needs to periodically carry out a short task. I have used handler with sendmessagedelayed to implement loop. It works but is there a better way?
@Override
public boolean handleMessage(Message arg0) {
//do something
Message msgtx=Message.obtain();
handler.sendMessageDelayed(msgtx, updaterate);
return true;
}
If the task is executed, say, each X minutes or more, using a handler is OK. If the delay between task executions is bigger (hours or so), I recommend using the
AlarmManager: