23

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.

3 Answers3

33

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.

Yugandhar Babu
  • 10,311
  • 9
  • 42
  • 67
  • Sorry about that Yugandhar, I do have too questions in one, I have to stop doing that, SO is very confusing. –  Dec 22 '11 at 09:09
  • I have the exact same code as you in another activity and it is the one I would like to use but I cant kill the activity once it's started no matter how I try. I will have to put that in another post. THanks for your help! –  Dec 22 '11 at 09:11
  • but how you can distinguish the cases where onlocationchanged is not called because your location has not changed, from the cases when onlocationchanged is not called because gps signals are weak and a location fix can not be obtained ? – dariusiv Nov 15 '15 at 16:51
  • Even if you set `minTime` to 100ms, the `onLocationChanged()` method gets called after a second, I think by default, 1000ms is the min time you can set or if you set lower than this, 1000ms will be used behind the scenes. – CopsOnRoad Jul 02 '18 at 02:22
7

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

Tobias Breßler
  • 316
  • 3
  • 8
0
public void onLocationChanged(Location location)

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

Awais Tariq
  • 7,724
  • 5
  • 31
  • 54