4

This symptom has been covered often, but I have not found another question the same criteria so here goes...

I'm making an app that successfully uses GPS when the device has service. I have a testing device with 2.3.4 version Android. It has no SIM so GPS is not expected. However it can get location via Wifi in Google Maps. My app however can not get a location on this device. On a device with service, I can get the same behavior by putting the device into airplane mode and then reenabling Wifi.

In my app, I have a gps tracking class that implements LocationListener.

At application start up, the following code runs:

public Location getLocation() {
    try {
        locationManager = (LocationManager) mContext
                .getSystemService(LOCATION_SERVICE);

        // getting GPS status
        isGPSEnabled = locationManager
                .isProviderEnabled(LocationManager.GPS_PROVIDER);

        // getting network status
        isNetworkEnabled = locationManager
                .isProviderEnabled(LocationManager.NETWORK_PROVIDER);

        if (!isGPSEnabled && !isNetworkEnabled) {
            // no network provider is enabled
        } else {
            this.canGetLocation = true;
            // First get location from Network Provider
            if (isNetworkEnabled) {
                Log.d(LOG_TAG, "Requesting updates");
                //TODO once tested, up the time below
                locationManager.requestLocationUpdates(
                        LocationManager.NETWORK_PROVIDER,
                        MIN_TIME_BW_UPDATES,
                        MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                Log.d("Network", "Network");
                if (locationManager != null) {
                    location = locationManager
                            .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                    // *** location is null here ***
                    if (location != null) {
                        latitude = location.getLatitude();
                        longitude = location.getLongitude();
                    }
                }
            }
            // if GPS Enabled get lat/long using GPS Services
            if (isGPSEnabled) {
                if (location == null) {
                    locationManager.requestLocationUpdates(
                            LocationManager.GPS_PROVIDER,
                            MIN_TIME_BW_UPDATES,
                            MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                    Log.d("GPS Enabled", "GPS Enabled");
                    if (locationManager != null) {
                        location = locationManager
                                .getLastKnownLocation(LocationManager.GPS_PROVIDER);
                        if (location != null) {
                            latitude = location.getLatitude();
                            longitude = location.getLongitude();
                        }
                    }
                }
            }
        }

    } catch (Exception e) {
        e.printStackTrace();
    }

    return location;
}

See comment "location is null here". This is the case at start up and subsequent calls minutes later.

None of these interface functions are ever called: onLocationChanged onProviderEnabled onProviderDisabled onStatusChanged

Have I muffed something basic? It seems like location should be attainable at least after a bit of wait. And, as mentioned, Google maps is working in wifi only mode on both devices.

Mark Murphy
  • 1,580
  • 1
  • 16
  • 29
  • what are you saying that GPS without a SIM is not expected? GPS should work just fine (and as suggested/confirmed by [this question](http://stackoverflow.com/q/4375913/250725)), although without service A-GPS is impossible, so connection times might be slow. – psubsee2003 Dec 14 '12 at 00:06

1 Answers1

3

It turned out that the acquisition of location was just taking much longer than expected. About 2 minutes I think.

Mark Murphy
  • 1,580
  • 1
  • 16
  • 29
  • I encountered the same issue on an app I developed. When the device has no service, GPS and NETWORK locations take longer to acquire. 2 minutes or longer. I would suggest you try and implement your app for devices with SIM to expect the needed performance. – Andres Dec 22 '12 at 23:11