Reputation: 1660
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.
Upvotes: 4
Views: 5683
Reputation: 1660
It turned out that the acquisition of location was just taking much longer than expected. About 2 minutes I think.
Upvotes: 3