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

本文介绍了一种使用Timer+TimerTask+Handler+LocationManager实现的定时间隔定位方法,并详细解析了为何要在TimerTask中通过Handler来请求位置更新。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

采用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.


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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值