位置广播接收器
我知道 BroadcastReceiver 会监视文本、电话事件等...但是您可以将 LocationServices 作为基于位置的服务和事件运行吗?
例如,您位于某个 GPS 点附近,手机会通知您。
I know BroadcastReceiver watches for text, phone events, etc... but can you run LocationServices as a service and event based on Location?
For example, you are near a certain GPS point and the phone notifies you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我想你正在寻找的是这样的东西。
LocationManager.requestLocationUpdates()
的一个版本采用PendingIntent
作为参数,并且此 Intent 可用于触发BroadcastReceiver
。此代码将注册一个自定义广播以随位置更新一起触发。然后,您只需使用相同的 Intent 操作注册您的
BroadcastReceiver
,它将接收位置更新以进行处理。当然,请保留PendingIntent
,因为您需要在某些时候调用manager.removeUpdates(launchIntent)
!最后说明:
因为听起来您正在尝试在应用程序不在前台时实现定期位置更新,所以请记住这一点。除非您希望您的应用程序被打上电池杀手的标签,否则请在实现此功能时务必非常小心。
您将需要使用较大的 minTime 或 minDistance 参数来大大降低位置更新的频率,以允许定位服务休息。此功能还应该附加到用户控制的首选项,以便他们可以启用/禁用您的应用程序在后台跟踪位置。
希望有帮助!
I think what you are looking for is something like this. There is a version of
LocationManager.requestLocationUpdates()
that takes aPendingIntent
as a parameter, and this Intent could be used to fire aBroadcastReceiver
. This code will register a custom broadcast to fire with the location updates.Then you just have to register your
BroadcastReceiver
with the same Intent action and it will receive the location updates for processing. And, of course, keep thatPendingIntent
around because you'll need to callmanager.removeUpdates(launchIntent)
at some point!Final Note:
Because it sounds like you are trying to implement regular location updates while your app isn't in the foreground, keep this in mind. Unless you want your app branded as a battery killer, be extremely careful with how you implement this functionality.
You will want to greatly reduce the frequency up location updates with either a large minTime or minDistance parameter to allow the location service to rest. This feature should also be attached to a user controlled preference so they can enable/disable your app from tracking location in the background.
Hope that Helps!
这个问题已经有两年多了,但现在有一种非常非常简单且电池高效的方法来做到这一点。这个概念称为地理围栏。您可以在下面更详细地了解它:
http://developer.android.com/training/位置/地理围栏.html
This question is more then 2 years old but there is now very very easy and battery efficient way to do this. The concept is called Geofences. You can learn it in more detail below:
http://developer.android.com/training/location/geofencing.html