Android when exactly is onLocationChanged called-ThrowExceptions

Exception or error:

It seems for me it is getting called the first time the activity starts, just after onCreate, it then seems to be called at random intervals, whether I move or not???

Regardless of that is it simply called automatically if I have code like this in the onCreate method?

locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
Criteria criteria = new Criteria();

Is that right???

Cheers,
Mike.

How to solve:

Your question is not clear initially.Your code and title are not matching. I am giving answer for your title only.

You have to register Location Listener for your Location Manager, then only onLocationChanged() will be called according the settings you supplied while registering location listener.

See below code how to do that. I used GPS Provider, you can use any provider based on criteria also.

LocationManger lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 0, new LocationListener() {
            @Override
            public void onLocationChanged(Location location) {
                // TODO Auto-generated method stub
            }
            @Override
            public void onProviderDisabled(String provider) {
                // TODO Auto-generated method stub
            }
            @Override
            public void onProviderEnabled(String provider) {
                // TODO Auto-generated method stub
            }
            @Override
            public void onStatusChanged(String provider, int status,
                    Bundle extras) {
                // TODO Auto-generated method stub
            }           
        });

Coming to your question, onLocationChanged() will be called if the current location update is not matching with last known location.

The updated location will be changed for every minTime (in my case 1000 milli sec) and also if device moved minDistance (in my case 0 meters) distance.

I hope you will understand this.

###

if you want to catch new locations, you have to register a LocationListener like this:

LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
LocationListener listener = new LocationListener() {
   ...
}
locationManager.requestLocationUpdates(GPS_PROVIDER, intervall, distance, listener);

With intervall and distance you can configure:

  1. If intervall is greater than 0, the LocationManager could potentially rest for intervall milliseconds between location updates
  2. If distance is greater than 0, a location will only be broadcasted if the device moves by distance meters.

When the LocationListener is registered, the LocationManager starts to get your geo location and calls the onLocationChanged(). If the distance is very low, it can happen that the method is called very often in a short period of time. According to the intervall, the LocationManager will rest afterwards.

I think, the LocationManager will only start doing it’s work, when a LocationListener is registered.

Hope that helps…

Cheers,
Tobi

###

public void onLocationChanged(Location location)

the above method gets called automatically once your location has been changed..

Leave a Reply

Your email address will not be published. Required fields are marked *