Change the criteria to receive location information

You can use an instance of the LocationProvider class to automatically change the criteria that is used to track the location of a BlackBerry® device.

  1. Import the required classes and interface.
    import net.rim.device.api.gps.GPSInfo;
    import javax.microedition.location.*;
  2. Create a class and constructor.
    public class handleGPS
    {
       public handleGPS(int gpsMode)
       {
       }
    }
  3. In the class, define static fields for the location provider, latitude, longitude, altitude, speed and course.
    static LocationProvider locationProvider;
    static double lat, lon;
    static float  alt, spd, crs;
  4. In the constructor, add a code block to set up a LocationProvider instance to switch to a different method of location tracking. Invoke reset() on the LocationProvider object, and then set the location listener to null to disable the listener.
    if (locationProvider != null)
    {
        locationProvider.reset();
        locationProvider.setLocationListener(null, -1, -1, -1);
    }
  5. In the constructor, create and configure a Criteria object based on the GPS mode that is passed as a parameter to the constructor.
    Criteria myCriteria = new Criteria();
    myCriteria.setPreferredResponseTime(Criteria.NO_REQUIREMENT);
    myCriteria.setCostAllowed(true);
    
    if ( gpsMode == GPSInfo.GPS_MODE_AUTONOMOUS )
    {
        myCriteria.setCostAllowed(false);
    }
    else if ( gpsMode == GPSInfo.GPS_MODE_ASSIST )
    {
        myCriteria.setPreferredPowerConsumption(Criteria.POWER_USAGE_MEDIUM);
    }
    else
    {
        myCriteria.setPreferredPowerConsumption(Criteria.POWER_USAGE_LOW);
    }
  6. In the constructor, create a try/catch block. In this block, create an instance of the LocationListener class by invoking getInstance() and passing the Criteria object as a parameter. Specify a location listener to handle the GPS location updates.
    try
    {
       locationProvider = LocationProvider.getInstance(myCriteria);
    
       if (locationProvider != null)
       {
          locationProvider.setLocationListener
            (new myLocationListener(), -1, -1, -1);
       }
    }
    catch (Exception err)
    {}
  7. In the class, create a private static class that implements the LocationListener interface. Retrieve the current location information in the locationUpdated() method. Create a basic implementation of the providerStateChanged() method to monitor the LocationProvider state.
    private static class myLocationListener implements LocationListener
    {
       public void locationUpdated(LocationProvider provider, Location location)
       {
          lat = location.getQualifiedCoordinates().getLatitude();
          lon = location.getQualifiedCoordinates().getLongitude();
          alt = location.getQualifiedCoordinates().getAltitude();
          spd = location.getSpeed();
          crs = location.getCourse();
       }
    
       public void providerStateChanged(LocationProvider provider, int newState)
       {}
    }

Was this information helpful? Send us your comments.