In the code below getLastKnownLocation() always returns null on a device, even when I am getting Provide = internet. It was working fine but know it’s returning a null value. I am using a Galaxy Tab version 2.2.
public void find_Location(Context con) {
Log.d("Find Location", "in find_location");
this.con=con;
String location_context = Context.LOCATION_SERVICE;
LocationManager locationManager =
(LocationManager)con.getSystemService(location_context);
List<String> providers = locationManager.getProviders(true);
for (String provider : providers) {
locationManager.requestLocationUpdates(provider, 1000, 0,new LocationListener() {
public void onLocationChanged(Location location) {}
public void onProviderDisabled(String provider){}
public void onProviderEnabled(String provider){}
public void onStatusChanged(String provider, int status,Bundle extras){}
});
Location location = locationManager.getLastKnownLocation(provider);
if (location != null) {
lat = location.getLatitude();
lng = location.getLongitude();
Geocoder geocoder = new Geocoder(AdvanceSearch.this,
Locale.getDefault());List<Address> addresses;
try {
addresses = geocoder.getFromLocation(lat,lng,100);
countryname=addresses.get(0).getCountryName();
eexit e = new eexit();
statename= addresses.get(0).getAdminArea();
cityname=addresses.get(0).getLocality();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
Thanks in advance for your help.
I would focus on this line first. Your
requestLocationUpdateshas aminDistanceset at 0.Play around with the
minDistance, I think that is where your problem is.Edit:
In addition I would add a
Logto yourLocationListenermethods. Doing this has helped me quite a bit.Here is an example of my early
LocationListenerscript: