Reputation: 373
I have a function that sends location updates to my server and i need it to run all the time on set intervals. I know all processes can be killed by the system, but i need a way to make it restart by itself when killed. I know repeating event can be done either with Timer or AlarmManager, but which is better in my case, and how do i create a service that will be restarted if the system kills it? I'm beginner with android so please use detailed code/explanation.
This is the code i'm using to repeat my function now, but it gets killed when i close the application and if it's not killed, doubles the function so it sends two/three/four etc. updates every 5 minutes:
Timer timer = new Timer();
timer.schedule(new TimerTask() {
public void run() {
//do a barrel roll
}
}, 0, 300000);//repeat every 5 minutes
Upvotes: 0
Views: 312
Reputation: 13541
You can Send yourself a PendingIntent to start the service via an Alarm that you register with the AlarmManager. Note that an alarm can wake your process if its not awake (read the documentation for details)
http://developer.android.com/reference/android/app/AlarmManager.html http://developer.android.com/reference/android/app/PendingIntent.html
Upvotes: 1