Code sample: Retrieving the GPS location of a BlackBerry device by using continuous fix requests

import javax.microedition.location.*;

public class handleGPS
{
    public handleGPS()
    {
        Criteria myCriteria = new Criteria();

        try
        {
            LocationProvider provider = LocationProvider.getInstance(myCriteria);
            provider.setLocationListener(new handleGPSListener(), 10, -1, -1);
        }
        catch ( LocationException lex )
        {
            return;
        }
    }

    public static class handleGPSListener implements LocationListener
    {
        public void locationUpdated(LocationProvider provider, Location location)
        {
            if (location.isValid())
            {
                // do something
            }
            else
            {
                // invalid location
            }
        }

        public void providerStateChanged(LocationProvider provider, int newState)
        {
            if (newState == LocationProvider.OUT_OF_SERVICE)
            {
                // GPS unavailable due to IT policy specification
            }
            else if (newState == LocationProvider.TEMPORARILY_UNAVAILABLE )
            {
                // no GPS fix
            }
        }
    }
}

Was this information helpful? Send us your comments.