Retrieve the location of a BlackBerry device by specifying continuous fix requests

You can use the Location API to retrieve location information of a BlackBerry® device at any interval.
  1. Import the required classes and interface.
    import javax.microedition.location.*;
  2. Create a class and a constructor.
    public class handleGPS
    {
        public handleGPS()
        {
        }
    }
  3. In the constructor, create an instance of the Criteria class. Create a try/catch block. In this block, create an instance of the LocationProvider class by invoking getInstance() and using the Criteria object. Invoke setLocationListener() to specify the location of the GPS event listener.
    Criteria myCriteria = new Criteria();
    
    try
    {
        LocationProvider provider = LocationProvider.getInstance(myCriteria);
        provider.setLocationListener(new handleGPSListener(), 10, -1, -1);
    }
    catch ( LocationException lex )
    {
        return;
    }
  4. In the class, implement the LocationListener interface. You must add functionality as required to this implementation.
    public static class handleGPSListener implements LocationListener
    {
       public void locationUpdated(LocationProvider provider, Location location)
       {
          if (location.isValid())
          {
              // do something
          }
          else
          {
              // invalid locatuon
          }
       }
    
       public void providerStateChanged(LocationProvider provider, int newState)
       {
          if (newState == LocationProvider.OUT_OF_SERVICE)
          {}
          else if (newState == Location.TEMPORARILY_UNAVAILABLE )
          {}
       }
    }

Was this information helpful? Send us your comments.