I made a little gps app for some reason onLocationChanged not refreshing, it only run once, on app start.
Here is my code:
public BackgroundLocationService() {
super("myintentservice");
locManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
Criteria crt = new Criteria();
crt.setAccuracy(Criteria.ACCURACY_FINE);
String bestProvider = locManager.getBestProvider(crt, true);
boolean gps_enabled;
boolean network_enabled;
boolean best_enabled;
gps_enabled = locManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
network_enabled = locManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
best_enabled = locManager.isProviderEnabled(bestProvider);
if (best_enabled) {
locManager.requestLocationUpdates(bestProvider, 15000, 20, locListener);
Log.i(TAG, "Best enabled: " + bestProvider);
} else {
Log.i("Location Provider: ", "best not enabled: " + bestProvider);
if (gps_enabled) {
locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locListener);
Log.i(TAG, "gps enabled!");
}
if (network_enabled) {
locManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locListener);
Log.i(TAG, "network enabled!");
}
}
}
class MyLocationListener implements LocationListener {
public void onLocationChanged(Location location) {
if (location != null) {
//locManager.removeUpdates(locListener);
longitude = Double.toString(location.getLongitude());
latitude = Double.toString(location.getLatitude());
Log.i(TAG,"Location has CHANGED!");
}
}
public void onProviderDisabled(String arg) {}
public void onProviderEnabled(String arg) {}
public void onStatusChanged(String provider, int status, Bundle extras) {}
}
The “Location has CHANGED!” message only shows once with each app start.
Im in an office but i can move at least 5-10 meters away from my position and ALL the providers are enabled so it must be Ok, isnt it?
Any ideas ?
When you register the location listener, you’re requesting updates with a min time of 15 seconds and a minimum distance of 20 meters. Since you’re inside an office, it’s unlikely that you’re able to get an accurate enough GPS fix to actually detect a move of 5-10 meters. Have you tried debugging outside? Similarly, if you enabled mock locations, send mock coordinates to the emulator to ensure your app acts as you expect when locations are updated.