简体   繁体   English

每隔X秒运行AsyncTask以更新GPS位置

[英]Run AsyncTask every X second to update GPS position

I have a class that extends AsyncTask , which fetches the gps cordinates from the device. 我有一个扩展AsyncTask的类,该类从设备获取gps坐标。

I would like to keep the data updated, so my initial though was to call the class from a timer or a handler. 我想保持数据的更新,因此我的第一步虽然是从计时器或处理程序中调用该类。 Is this a smart way to implement it, or am i better off listening to the onLocationChanged and do my updates in there? 这是实现它的明智方法,还是我最好听一下onLocationChanged并在那里进行更新?

Hope you get the idea, otherwise ill elaborate. 希望您能理解,否则请多加说明。

Thanks! 谢谢!

An alarmManager will be a good solution here. 在这里, alarmManager将是一个很好的解决方案。

These allow you to schedule your application to be run at some point in the future. 这些使您可以计划您的应用程序在将来的某个时间运行。

When an alarm goes off, the Intent that had been registered for it is broadcast by the system, automatically starting the target application if it is not already running. 警报响起时,系统会广播已为其注册的Intent,并在目标应用程序尚未运行时自动启动它。

So when alarm gets triggered, call your execute() method of Async task. 因此,当触发警报时,请调用Async task. execute()方法Async task.

For more info see this: http://developer.android.com/reference/android/app/AlarmManager.html 有关更多信息,请参见: http : //developer.android.com/reference/android/app/AlarmManager.html

I also want to implement the same in my app in near future. 我也想在不久的将来在我的应用程序中实现相同的功能。 If you get the solution, don't forget to update the post about how you implemented it. 如果您找到了解决方案,请不要忘记更新有关如何实现它的帖子。

Thank you. 谢谢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM