-1

我有下面的代码,它得到了Most Recent location之后的 every30 seconds10 meters,但它会不断地接收位置更新,并且每 30 秒后它会获取最新的位置更新。

有没有更好/有效的方法来做到这一点?

基本上我的要求是Current Location每 30 秒和10 meters distance moved. 我可以通过多种方式做到这一点,我在想有没有更好的方法来解决这个问题?那么 Timer 类呢?

任何想法都会有很大帮助。

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);    

    locationListener = new GPSLocationListener();

    locationManager.requestLocationUpdates(
            LocationManager.GPS_PROVIDER, 
            30000, 
            10, 
            locationListener);

}

下面是实现的类LocationListener

private class GPSLocationListener implements LocationListener {
    @Override
    public void onLocationChanged(Location location) {
        if (location != null) {
            GeoPoint point = new GeoPoint(
                    (int) (location.getLatitude() * 1E6), 
                    (int) (location.getLongitude() * 1E6));
        System.out.println(location.getLatitude()+"--"+location.getLongitude());

        }
    }

    @Override
    public void onProviderDisabled(String provider) {
    }

    @Override
    public void onProviderEnabled(String provider) {
    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
    }
}
4

1 回答 1

0

Techgeeky, not sure what you exactly want. But what you have is exactly what you describe you want, that is to get a nupdate every 30 sec if you've moved 10 meters. The OS will let the GPS chip sleep for 30 seconds and once woken up, if you've moved 10 meters, it'll give an update. What you have should work perfectly so what exactly is the issue? I've written multiple apps that use the location listener and waking up every 30 seconds is reasonable for sure to get an update.

于 2012-09-17T04:57:23.260 回答