Reputation: 17
my
locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
returns true but on location changed get never called. my wifi is on , GPS Satellites & wifi and network location is on I can not make why it is not getting called. I have attached the required code below.
try
{
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
gps_enabled=locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
network_enabled=locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if(!gps_enabled && !network_enabled)
Toast.makeText(MainActivity.this, "Error Getting Your Location", Toast.LENGTH_LONG).show();
else
{
if(network_enabled)
{
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this);
Toast.makeText(MainActivity.this, "network_enabled", Toast.LENGTH_LONG).show();
}
if(gps_enabled)
{
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
Toast.makeText(MainActivity.this, "gps_enabled", Toast.LENGTH_LONG).show();
}
}
}
catch(Exception ex){}
@Override
public void onLocationChanged(Location location) {
Toast.makeText(MainActivity.this, "onLocationChanged", Toast.LENGTH_LONG).show();
this.lat = location.getLatitude();
this.lon = location.getLongitude();
(new CityList()).execute();
}
here the toast gps_enabled but onLocationChanged never appears. Any one please help me to rectify the things.
Upvotes: 0
Views: 944
Reputation: 26
MAYBE the problem is not in this part of code.
First thing is, you can not simulate location change on emulator. You have to test it on real device, preferably outdoor.
To get the proper position using GPS, normally you have to wait sometimes 10-60 seconds (or more). It means that if you use a background service (as I did with AlarmManager) to get GPS location you have to wait some time before closing it.
Upvotes: 1