Implementing location detection

LocationFinder

The LocationFinder class wraps the Location API functionality in the MIDlet so that devices that do not support the API can nevertheless run the MIDlet.

The getFinder method checks if the Location API is supported on the device and returns a LocationFinder object. If the Location API is not supported, the method returns null.

    public static LocationFinder getFinder(Listener listener) {
        if (listener == null) {
            throw new NullPointerException("listener not defined");
        }

        LocationFinder finder = null;
        try {
            // this will throw an exception if JSR-179 is missing
            Class.forName("javax.microedition.location.Location");

            if (finder == null && supportsGPS()) {
                Class c =
                    Class.forName(
                    "com.nokia.example.attractions.location.GpsLocationFinder");
                finder = (LocationFinder) (c.newInstance());
                try {
                    finder.init(listener);
                }
                catch (InitializationException e) {
                    finder = null;
                }
            }
            if (finder == null && supportsCellId()) {
                Class c =
                    Class.forName(
                    "com.nokia.example.attractions.location.CellIdLocationFinder");
                finder = (LocationFinder) (c.newInstance());
                try {
                    finder.init(listener);
                }
                catch (InitializationException e) {
                    finder = null;
                }
            }
        }
        catch (Exception e) {
            finder = null;
        }
        return finder;
    }

CellIdLocationFinder

The CellIdLocationFinder class provides a location provider that uses cell ID positioning to determine the device location.

The getLocationProvider method returns an instance of the location provider.

    protected final LocationProvider getLocationProvider()
        throws LocationException, SecurityException {
        // Prompt first for network connection
        // to prevent bugs in Cell ID service.
        HttpConnection conn = null;
        try {
            conn = (HttpConnection) Connector.open(TEST_URL);
        }
        catch (SecurityException e) {
            throw e;
        }
        catch (IOException e) {
        }
        finally {
            if (conn != null) {
                try {
                    conn.close();
                }
                catch (IOException e) {
                }
            }
        }

        int[] methods = {(Location.MTA_ASSISTED | Location.MTE_CELLID
            | Location.MTE_SHORTRANGE | Location.MTY_NETWORKBASED)};
        return LocationUtil.getLocationProvider(methods, null);
    }

GpsLocationFinder

The GpsLocationFinder class provides a location provider that uses GPS to determine the device location.

The criteria for the location provider are specified in the class constructor.

    private final Criteria criteria;

    GpsLocationFinder() {
        criteria = new Criteria();
        criteria.setCostAllowed(true);
        criteria.setPreferredPowerConsumption(Criteria.NO_REQUIREMENT);
        criteria.setSpeedAndCourseRequired(false);
        criteria.setAltitudeRequired(false);
        criteria.setAddressInfoRequired(false);
    }

The getLocationProvider method returns an instance of the location provider.

    protected final LocationProvider getLocationProvider()
        throws LocationException {
        return LocationProvider.getInstance(criteria);
    }