问题
I am quite new to Android, and I am really wondering about is how to loop or to repeat periodically a task. In my program, I have UpdateLoc() that sends my gps location to my databse, but I want it to update periodically (whether it be 3 min or 3 hours) without using too much battery and CPU. The problem is that I have no idea where to start... Is there a simple way to do this?
回答1:
I believe you have two options in this case:
- Service - This will stay running in the background but use more battery.
- AlarmManager - You can schedule a task to run in the future to briefly perform an update operation and should use less battery.
回答2:
You might want to look here: http://developer.android.com/reference/android/location/LocationListener.html http://developer.android.com/reference/android/location/LocationManager.html#requestLocationUpdates(java.lang.String, long, float, android.location.LocationListener)
This will allow you to receive updates only after a particular amount of time has past.
来源:https://stackoverflow.com/questions/8648724/how-to-loop-or-to-repeat-periodically-task-in-android