首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > 移动开发 >

采取Timer+TimerTask+Handler+LocationManager这种定时的间隔定位

2012-09-10 
采用Timer+TimerTask+Handler+LocationManager这种定时的间隔定位采用Timer+TimerTask+Handler+LocationMa

采用Timer+TimerTask+Handler+LocationManager这种定时的间隔定位

采用Timer+TimerTask+Handler+LocationManager这种定时的间隔定位时,在TimerTask中用handler发sendMessage,然后在Handler的handleMessage里面处理message并进行定位。这种方式是完全合理且正确的,
因为我也查了查,有人是这样说的:

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.
来自于http://www.questionhub.com/StackOverflow/3589963

?
于是,我又查了查官方文档中Reference对LocationManager的方法requestLocationUpdates(String provider, long minTime, float minDistance, LocationListener listener)的解释,有这么一句话,如下:

The calling thread must be a Looper thread such as the main thread of the calling Activity.

所以,正好在Activity里面创建一个Handler时,就满足了上面的要求,创建一个没有形参的Handler时,文档中是这么解释的,如下:

Default constructor associates this handler with the queue for the current thread.


综上所述,这些是我能解释和查到的。

热点排行