【发布时间】:2016-04-22 12:06:10
【问题描述】:
我有一个使用 GPS-Tracker 的离线地图应用程序。我的主要目标是感知纬度和经度的变化并更新TextView。
我的GPS-Tracker 类实现如下:
public class GPSTracker extends Service implements LocationListener {
...
public GPSTracker(Context context) {
...
getLocation();
}
public Location getLocation() {
// returns Location
}
@Override
public void onLocationChanged(Location location) {
// Method 1: update UI elements (textview) directly
MainActivity.tvGPS.setText("lat: " + Double.toString(location.getLatitude()) + "lon: " + Double.toString(location.getLongitude());
// Method 2: call a method from MainActivity which updates UI
MainActivity.setNewPosition(location.getLatitude() , location.getLatitude());
// Method 3: sense changes in Latitude and Longitude directly in MainActivity and update UI
}
}
问题:
如何直接感知经纬度的变化(MainActivity)并修改 UI。
我到目前为止测试的内容:
LocalBroacastManager 提到here 但它需要不断检查位置以检测更改
Observer Pattern 提到的here:更改仅在Observer 中有效@ 类不在MainActivity
【问题讨论】:
-
在这里查看我的答案:stackoverflow.com/a/36665760/4706693 在您的情况下,您可以在
onLocationChanged()中发送广播 -
是的,它有效,谢谢
标签: android