I am trying to get the users current location using the LocationManager. I have done a lot of research and can\'t seem to find anyone with the same problem. T
Sorry for bumping such an old thread, but this might come handy to some others, since this question is ranking relatively high on google search.
I had a similar problem, the onlocationchanged wasn't getting fired, but I could not find anything wrong with my code. After trying all sorts of combination, I realised that the root cause of the problem was the fact that I was registering the listener in a service.
The service WAS a foreground service, so based on Google documentation that should work, it DID NOT!
Wrapping the initialisation in a method, and calling the method from the activity which created or bind to the service solved my problem.
This is my method in my service:
void start_gps_track(int i) {
if (myCarLocationListener!=null) {
if (i==1) //if start night
myCarLocationListener.setNightstart(true);
else if (i==2) //if start GPS
myCarLocationListener.setLocationStart(true);
return;
}
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
if ( locationManager.isProviderEnabled( LocationManager.GPS_PROVIDER ) ) {
myCarLocationListener=new CarLocationListener(this,adjust_audio);
if (i==1) //if start night
myCarLocationListener.setNightstart(true);
else if (i==2) //if start GPS
myCarLocationListener.setLocationStart(true);
try {
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 100, 0, myCarLocationListener);
Log.d("HU","Registered location listener");
Location lastlocation = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (lastlocation == null)
lastlocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (lastlocation != null)
myCarLocationListener.onLocationChanged(lastlocation);
}
catch (SecurityException e){
Log.e("HU-GPS","No permission to access Location");
}
}
else
{
Log.e("HU-GPS","No GPS provider or no permission");
}
}
If I call this from within the Service itself I won't receive any Location Updates (again note service is a FOREGROUND service and it's actually binded to an activity which is currently running).
However if I call this method from the activity which created/bound to the service like below it works fine:
@Override
public void onServiceConnected(ComponentName className,
IBinder service) {
// We've bound to LocalService, cast the IBinder and get LocalService instance
TransporterService.LocalBinder binder = (TransporterService.LocalBinder) service;
mService = binder.getService();
mService.updatePlayer(player.this);
try {
mService.start_gps_track(0);
}
catch (Exception e) { //This should only happen if there is no GPS provider available, but it might happen quickly after boot when the device is not yet ready.
mService.recheck_gps(0);
}
mBound = true;
}
@Override
public void onServiceDisconnected(ComponentName arg0) {
mBound = false;
}
};
Hope this helps some others who come across this problem.