我正在使用以下代码轮询设备的当前位置 位置loc = LocationManager.getLocationManager()。getCurrentLocation();
每次返回相同的位置坐标。我必须使用getCurrentLocationSync()吗?请告知可能出错的地方。
另外,我应该使用PRIORITY_HIGH_ACCUARCY或PRIORITY_MEDIUM_ACCUARCY而不是“PRIORITY_LOW_ACCUARCY”
代码:
public class TrackLocation implements LocationListener {
private static TrackLocation instance = new TrackLocation();
private Location lastLocation;
private Timer time;
public static TrackLocation getInstance() {
return instance;
}
public void stopTracking() {
if (time != null) {
time.cancel();
}
time = null;
LocationManager.getLocationManager().setLocationListener(null);
}
public void startTracking() {
if (time != null) {
stopTracking();
}
if (Preferences.get("LocationTracking", true)) {
long delay = Server.instance.getLoctionPollingIntervalMillis();
LocationManager.getLocationManager().setLocationListener(this,
new LocationRequest(LocationRequest.PRIORITY_LOW_ACCUARCY, delay));
time = new Timer();
time.schedule(new TimerTask() {
@Override
public void run() {
if (lastLocation != null) {
Server.instance.updateLocationInServer(lastLocation.getLatitude(), lastLocation.getLongitude(), System.currentTimeMillis(), true);
}
}
}, 30000, delay);
}
}
@Override
public void locationUpdated(Location location) {
lastLocation = location;
}
@Override
public void providerStateChanged(int newState) {
}
}