【发布时间】:2012-06-05 10:15:03
【问题描述】:
因此,众所周知的事实是 GPS 接收器需要一段时间(大约 10-12 秒)才能锁定卫星。使用 Android LocationListener 我想在位置更新开始时收到通知。例如:
我有一个带有以下代码的活动来实例化和调用 LocationListener
public void startLocationUpdates() {
try{
Common.locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
listener = new LocListener();
if(Common.locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
Common.locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, listener);
} else if(Common.locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
Common.locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, listener);
}
}catch(Exception e) {
e.printStackTrace();
}
}
这是 LocationListener onLocationChanged 的实现。
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
try{
Common.currentLocation = new String(String.valueOf(location.getLatitude()) + "," + String.valueOf(location.getLongitude()));
Log.i("LocListener",Common.currentLocation);
} catch(Exception e) {
e.printStackTrace();
}
}
Common.currentLocation 字符串在 locationUpdates 开始后更新。但是,我想在我的主要活动中等待,直到该字段更新。
如果我在我的活动中放置一个 while 循环以等待更新 Common.currentLocation 字段,则线程将暂停并且不会调用 onLocationChanged 方法(不打印日志消息)。
一旦调用 onLocationChanged 并且纬度和经度值可用,从 onLocationChanged 方法获取更新到公共接口/活动的最佳方式是什么?任何帮助将不胜感激。
【问题讨论】:
-
您的位置监听器是否在您请求更新的同一活动中?
-
不,这是不同的课程。我应该在活动中将其设为私有内部类吗?
标签: android gps locationlistener