Reputation: 525
How can I set a timer, within a service that is running in foreground, so it runs a piece of code every 1 min. In pseudo code I want smth like this.
public int onStartCommand(Intent intent, int flags, int startId) {
startEveryMinTask()
return START_STICKY;
}
private void startEveryMinTask() {
//do stuf
}
Upvotes: 1
Views: 3304
Reputation: 13815
Just Use AlarmManager to shoot an intent to invoke your service again after its done with its code.
Upvotes: 0
Reputation: 1282
You can use java.util.Timer
Timer timer = new Timer();
public int onStartCommand(Intent intent, int flags, int startId) {
timer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
startEveryMinTask();
}, 60000, 60000); // 60000 milliseconds = 1 minute
return START_STICKY;
}
Upvotes: 3