private void getLocation(){
locationManager = (LocationManager) getContext().getSystemService(Context.LOCATION_SERVICE);
List providers = locationM
I believe you are interpreting wrong how the LocationManager works.
When you call locationManager.requestLocationUpdates(bestProvider, 0, 0, locationListener)
you are just registering to receive location updates, which normally takes some time to occur (and may never occur, as CommonsWare pointed out). This call does not gives you right away an updated location, so, in the next line of code, when you call getLastKnownLocation()
, it's normal behavior to receive null
.
That said, I believe the best way to deal with your situation is this:
1 - First: check whether getLastKnownLocation()
returns null or not (if the chosen LocationProvider has already a recent location from your app or even from another app, it will return it for you here. But beware: this location could be very outdated!).
2 - If getLastKnownLocation()
returns null, then you will have no other option but to request a brand new location and wait for it to arrive, which will happen on the method onLocationChanged
in the LocationListener. So, to do that you have two options: (a) call requestSingleUpdate()
, which will return an updated location for you just one time, or (b) call requestLocationUpdates()
, which will register the locationManager to receive periodical location updates (and will consume more battery).
3 - In both options, when you receive a location update, the method onLocationChanged
in the LocationListener will be called and then you can call your loadBusinesses
from this method, with the brand new location received.
Hope this is clear ;)
----- EDIT -----
You should also request location permissions on runtime, before calling requestSingleUpdate
or requestLocationUpdates
. To do that, use this snippet:
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// If permission is not granted, request it from user. When user responds, your activity will call method "onRequestPermissionResult", which you should override
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION}, 1);
} else {
locationManager.requestSingleUpdate(bestProvider, locationListener, null)
}
If the permission was not granted before, the user will be prompted to grant it (or not). So, you should also @Override the method onRequestPermissionResult
in your activity, to check whether user granted the permission or not, and then respond properly. This is the method you should override:
@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
switch (requestCode) {
case 1:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// PERMISSION WAS GRANTED
} else {
// USER DID NOT GRANT PERMISSION
}
break;
}
}