I try to get the current Location of the User using LocationListener in a FragmentActivity (in order to user SupportMapFragment) that implements LocationListener.
This is my code :
private void setLocationManager() {
if (lm == null)
lm = (LocationManager) this
.getSystemService(Context.LOCATION_SERVICE);
// exceptions will be thrown if provider is not permitted.
try {
gps_enabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
} catch (Exception ex) {
}
try {
network_enabled = lm
.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
} catch (Exception ex) {
}
if (gps_enabled) {
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
Toast.makeText(HomeActivity.this, "Refresh GPS", Toast.LENGTH_SHORT)
.show();
} else {
lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0,
this);
Toast.makeText(HomeActivity.this, "Refresh NET", Toast.LENGTH_SHORT)
.show();
}
}
@Override
public void onLocationChanged(Location location) {
MyCurrentLocation = location;
Toast.makeText(
HomeActivity.this,
"Refresh == my lat : " + MyCurrentLocation.getLatitude()
+ " my long : " + MyCurrentLocation.getLongitude(),
Toast.LENGTH_SHORT).show();
}
@Override
public void onProviderDisabled(String provider) {
Toast.makeText(HomeActivity.this, provider, Toast.LENGTH_SHORT).show();
}
@Override
public void onProviderEnabled(String provider) {
Toast.makeText(HomeActivity.this, provider, Toast.LENGTH_SHORT).show();
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
The problem I have are:
And I never use : lm.removeUpdate method
A reboot of my Samsung Galaxy S3 did the job..
Hope it helps.