views:

119

answers:

1

Hi,

I'm using the code shown below to get an updated value for location every time a button is clicked. When my activity is resumed I get an update every second, so that when I call getLastKnownLocation I expect to have a location that have been updated in the last second.

Is that the correct way to do that?

I would expect the onLocationChanged event to be triggered every time I execute a 'geo fix' command (or max after 1s since I request update every 1s), but it's only triggered the first time. Why?

Any help/suggestion welcome!

Thanks

package org.digitalfarm.atable;

...

public class Atable extends Activity {
    private Button mSearchButton;
    private TextView mytext;
    private LocationManager locationManager;    


    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);   

        setContentView(R.layout.main);

        mSearchButton = (Button)this.findViewById(R.id.button);

        mytext = (TextView) findViewById(R.id.dude);

        locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
        final Criteria criteria = new Criteria();
        criteria.setAccuracy(Criteria.ACCURACY_FINE);               


        mSearchButton.setOnClickListener(new OnClickListener() {
          @Override
          public void onClick(View v) {

                  String provider = locationManager.getBestProvider(criteria, true);

                  Location location = locationManager.getLastKnownLocation(provider);             

          }
        });
    }

     //Start a location listener
    LocationListener onLocationChange=new LocationListener() {
        public void onLocationChanged(Location loc) {
            //sets and displays the lat/long when a location is provided
            String latlong = "Lat: " + loc.getLatitude() + " Long: " + loc.getLongitude();   
            mytext.setText(latlong);
        }

        public void onProviderDisabled(String provider) {
        // required for interface, not used
        }

        public void onProviderEnabled(String provider) {
        // required for interface, not used
        }

        public void onStatusChanged(String provider, int status,
        Bundle extras) {
        // required for interface, not used
        }
    };

    //pauses listener while app is inactive
    @Override
    public void onPause() {
        super.onPause();
        locationManager.removeUpdates(onLocationChange);
    }

    //reactivates listener when app is resumed
    @Override
    public void onResume() {
        super.onResume();
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,1000,100.0f,onLocationChange);
    }
}
A: 

The time, specified in requestLocationUpdates is the shortest possible time interval for which a maximum of one update will occur. So you have registered for at most one update every second, but the actual update may take more that that to occur, and, furthermore, no guarantee is given for that.

From the documentation:

minTime - the minimum time interval for notifications, in milliseconds. This field is only used as a hint to conserve power, and actual time between location updates may be greater or lesser than this value.

Try to set that time interval to 0 and if there are no other problems (although it all seems fine to me), you should start getting the updates "as frequently as possible".

Shade