LocationManager requestLocationUpdates和TimerTask在安卓 [英] LocationManager requestLocationUpdates and timertask in android
问题描述
我有以下的code:
if (gps_enabled) {
Log.e("$$$$$$$$$$$$$$",
"GPS is enabled requestion location updates... interval value is: "
+ interval);
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,
locationListenerGps);
}
else{
if (network_enabled) {
Log.e("$$$$$$$$$$$$$$",
"Network is enabled requestion location updates... interval value is: "
+ interval);
lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0,
0, locationListenerNetwork);
}
}
与code,我可以得到的位置(至少使用网络供应商!(在另一篇文章的另一个问题)) 我想获得通知,以规则的间隔说,每隔一小时,但传递的参数requestLocationUpdates不保证该区间将保持(至少是我的测试showned,因为我希望更新的每一分钟,但得到了很多更新,而不是之一!) 所以我想使用的TimerTask,并安排它,我现在有
with that code I can get the location(at least using the network provider! (another issue on another post)) I would like to get notifications in a regular interval say every one hour, but passing the parameter to requestLocationUpdates doesn't guarantee that the interval will be maintained (at least that my tests showned,since I expected update every minute but got a lot of updates instead of one!) so i thought of using a timerTask and schedule it, I now have
timer1.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
getLocation();
}
}, 0, 180000);// 3 minutesr...
在这里的getLocation是我叫previously调用,但是当计时器调用该方法,然后什么也没有发生,日志停留在这一点上
where getLocation is the method that I called previously called, but when the timer calls that method then nothing happens, the logs stop at this point
Log.e("$$$$$$$$$$$$$$",
"Network is enabled requestion location updates... interval value is: "
+ interval);
和我从来没有通知我的位置。 任何想法?
and I never get notified about my location. any Ideas?
推荐答案
好吧我花了一段时间,但我已经找到了解决办法,因为文件说,你只能从位置管理器请求位置更新从弯针线,这意味着,当计时器任务称为必须获取一个消息,并且将消息发送到一个处理程序和该处理程序将负责用于请求位置更新。
Ok took me a while but I have found the solution to this,as the documentation says you can only request location updates from the location manager from a looper thread, that means that when the timer task is called you have to obtain a message and send the message to a handler and the handler would be responsible for requesting location updates.
这篇关于LocationManager requestLocationUpdates和TimerTask在安卓的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!