-1

I have this below code which get the Most Recent location after every 30 seconds and 10 meters, but it will constantly keep on receiving the location updates and it picks up the most recent location updates after every 30 seconds.

Is there any better/efficient way to do this?

Basically my requirement is to make use of the Current Location after every 30 seconds and 10 meters distance moved. I can do this in several ways, I was thinking is there any better way to do this problem? And what about Timer class?

Any thoughts will be of great help.

@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);

}

Below is the class that implements 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) {
    }
}
arsenal
  • 23,366
  • 85
  • 225
  • 331
  • fetching gps co-ordinate on every 30 seconds is going to drain your battery faster. – Lucifer Sep 17 '12 at 00:33
  • @Lucifer, So what should be the ideal time? As I need to make use of the Current Location, so what time interval should I set? – arsenal Sep 17 '12 at 00:35
  • Possible duplicate of [Location Updates every 35 seconds and draw a circle on the current location](http://stackoverflow.com/questions/12451722/location-updates-every-35-seconds-and-draw-a-circle-on-the-current-location). – trashgod Sep 17 '12 at 00:40
  • That question I have posted is only taking about drawing a Circle on the current location after every 30 seconds. But in this I am trying to find out the efficient way to get the current location after every 30 seconds. So both of them are two different question. I am asking this so that I can get to know more about some other approach . And If needed, I should go ahead and make the necessary changes in my code. – arsenal Sep 17 '12 at 00:51
  • @TechGeeky, i told you the idea interval on previous question's comment. – Lucifer Sep 17 '12 at 02:22
  • @TechGeeky, i think this [question](http://stackoverflow.com/q/3145089/996493) would be very helpful for you :) – Lucifer Sep 17 '12 at 02:41

1 Answers1

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.

vkinra
  • 943
  • 1
  • 9
  • 16
  • Are you sure, it sleeps for 30 seconds? I don't think so. And I was thinking if there is any efficient way to do this thing apart from I did. – arsenal Sep 17 '12 at 05:32
  • Yes, I'm sure. You can test it very simply by seeing the GPS icon in the status bar disappear for 30 seconds on the device. Note that will only happen if no other app is requesting updates. The GPS chip will likely be put in low power mode by the OS if you're not requesting very frequently. What you've done is as efficient as you're going to get if you want an update every 30 seconds. – vkinra Sep 17 '12 at 15:29