Hi I got a service in android and I want it to update something every 10 seconds. So I was thinking of having a wait and then restart the function (cant think of what to call it). Any ideas on how to do a wait in a service?
Asked
Active
Viewed 281 times
3 Answers
1
Use an AlarmManager. This will run your service at your scheduled time, and stops the service when it's done, ready to start again via the AlarmManager.

nhaarman
- 98,571
- 55
- 246
- 278
1
You will want to use threading. This can be done with something like this.
public void onClick(View v) {
new Thread(new Runnable() {
public void run() {
//Your code for whatever goes in here
}
}).start();
}
You can then call thread.sleep(10000)
. The 10000 translates to 10 seconds.
If you look at the accepted answer on this post the posters code shows all of this in an excellent way to understand.