How to loop or to repeat periodically task in Android?

无人久伴 提交于 2019-12-13 18:01:37

问题


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:

  1. Service - This will stay running in the background but use more battery.
  2. 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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!