Registering listeners

LocationListener implementation can be registered to a particular location provider with LocationProvider's setLocationProvider method. With this it is possible to define the following parameters: the interval (in seconds) at which the application wants to receive events; the timeout value, which indicates how late (in seconds) an update is allowed to be in comparison to the defined interval; the maxage value, which defines how old the location update value is allowed to be. Code sample 5 demonstrates how the TouristRoute MIDlet registers the LocationListener

Code sample 5: Registering the location listener (TouristRoute's TouristData class)

if (provider != null)

  int interval = -1; // default interval of this provider  
  int timeout = 0; // parameter has no effect.
  int maxage = 0; // parameter has no effect.
  
  provider.setLocationListener(this,interval,timeout,maxage);

Code sample 6 shows the registration of ProximityListener by using LocationProvider's static addProximityListener method. With this method, for each listened coordinate one can also set a radius (in meters) that is a threshold value for event launching. In case the registration fails due to a lack of resources or support, LocationException is thrown. In the TouristRoute MIDlet, ProximityListener is registered to each landmark found from the LandmarkStore. Proximity listener registration method takes the Coordinates parameter, which can be obtained, for example, from the Landmark instance by using the getQualifiedCoordinates() method, same as in the TouristRoute example.

Code sample 6: Registering proximity listeners (TouristRoute's TouristData class)

try

    LocationProvider.addProximityListener(this,
                                          coordinates,
                                          PROXIMITY_RADIUS)

catch (LocationException e)

    // Platform does not have resources to add a new listener 
    // and coordinates to be monitored or does not support 
    // proximity monitoring at all 

Note: All devices and emulators might not support proximity event sending through the ProximityListener interface. If the target device has no support, listeners have no effect.