I want to make an activity that starts a (service or thread) that repeats the same commands continuously every x seconds.
How to I go about doing the following:
- The (service or thread) needs to run in the background even though the os destroys the activity.
- I only want 1 (service or thread) running even though my activity is destroyed and created multiple times
- When a user uninstall the activity, I would like to kill the (service or thread)
I would appreciate some pointers to lead me in the right direction.
In my opinion, you should use AlarmManager with an IntentService to schedule repeating background tasks. You can even have alarms wake up the phone to execute your code with AlarmManager.
See:
https://developer.android.com/reference/android/app/AlarmManager.html
http://mobile.tutsplus.com/tutorials/android/android-fundamentals-scheduling-recurring-tasks/
http://android-er.blogspot.in/2010/10/simple-example-of-alarm-service-using.html
In case of phone restart, you will need to trigger the alarm manager again. See this tutorial for exact instructions on how to do this:
http://www.androidenea.com/2009/09/starting-android-service-after-boot.html