我需要在整个应用程序中实现一个LocationListener。
我知道我可以使用LocationListener服务,但问题是我必须在位置更改时使用一些通知程序。当我能够随时获得最后一个位置时,这还不够,因为我需要在所有活动中立即改变当前位置。
所以在伪代码中我需要这样的东西:
myLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, new LocationListener() {
public void onLocationChanged(Location location) {
notifyAllActivities(location);
}
};
并在每个活动中:
void onNotify(Location location) { //... }
答案 0 :(得分:3)
您是否尝试过使用CommonsWare's LocationPoller library?
众所周知,它非常强大,我个人在我的一个基于位置的应用程序中使用它。
它的一个好处是它与使用Service
的兼容性;即使应用程序在后台,您也需要获取位置更新。它获取WakeLock
来唤醒手机,获取位置然后优雅地释放它。
答案 1 :(得分:2)
使服务调用sendBroadcast()以广播Intent。将经度/纬度作为额外内容添加到该Intent。在需要该位置的每个活动中,为intent注册一个BroadcastReceiver,并从接收者的onReceive()
中获取Intent的额外内容中的lng / lat。