OnLocationChanged callback is never called
GPS Provider will disable when device in low power or use power save mode then in that case we can not receive onLocationChanged
GPS Provider hardly ever return the location if you stay inside house or building (only work well in outdoor)
Therefore, in my app I will use both GPS_PROVIDER
and NETWORK_PROVIDER
to receive onLocationChanged()
better
https://developer.android.com/guide/topics/location/strategies.html#Updates
To request location updates from the GPS provider, use GPS_PROVIDER instead of NETWORK_PROVIDER. You can also request location updates from both the GPS and the Network Location Provider by calling requestLocationUpdates() twice—once for NETWORK_PROVIDER and once for GPS_PROVIDER.
It looks like you setup should work, since it doesn't, I would make your example as simple as possible in order to troubleshoot. I would make your request look like
requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
This way you get ALL the updates possible. And comment out the part about getting the last known location. It's not needed yet.
Then in onLocationChanged()
, just have
Log.v(TAG, "IN ON LOCATION CHANGE, lat=" + latitude + ", lon=" + longitude);
Comment out the rest so that you keep your listener active. This should give you a stream of updates on a real device. On the emulator, you'll need to use DDMS, and you'll get one GPS update each time you press send.
Replacing
LocationManager.GPS_PROVIDER
with
LocationManager.NETWORK_PROVIDER
solved my problem.
Here is code snippet of my location manager
LocationManager locationManager = (LocationManager) getActivity().getSystemService(LOCATION_SERVICE);
//Check for Location permissions, Marshmallow and above
if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
&& ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
Toast.makeText(getActivity(), "Not Enough Permission", Toast.LENGTH_SHORT).show();
return;
}
//Get current location to start with
Location myLocation = locationManager.getLastKnownLocation(LocationManager.PASSIVE_PROVIDER);
currentLatitude = myLocation.getLatitude();
currentLongitude = myLocation.getLongitude();
// Request location update using LocationManager.NETWORK_PROVIDER
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, MapFragment.this);
As Steve Blackwell wrote, your setup is good. What you can try is to understand if you 100% have a GPS fixed signal! I have installed a nice GPS Tester application (from the play store) and it will show you if you have a fix or not and to which satellites you are connected and what is the connection strength. This was the reason my OnLocationChanged() never called.. I tried running it from within a building :\
good luck!