Get Location As It Changes
I'm trying to get the location of the user as soon as it changes. But the onLocationChanged method is never called. I've tried multiple solutions or try to look for some other code
Solution 1:
You can use BroadcastReceiver to detect whenever location is changed:
android.location.Location location;
privatestaticfinallongMIN_DISTANCE_CHANGE_FOR_UPDATES=100; // 100 meter// The minimum time beetwen updates in millisecondsprivatestaticfinallongMIN_TIME_BW_UPDATES=15*1000; // 15 secondBroadcastReceivergpsLocationChangeReciever=newBroadcastReceiver() {
@OverridepublicvoidonReceive(Context context, Intent intent) {
if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER) || locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
// if location is changed
setLocation(getLocation());
} else {
//
}
}
};
and here is the getLocation method which is called inside broadcast :
public android.location.Location getLocation() {
try {
locationManager = (LocationManager) mContext
.getSystemService(LOCATION_SERVICE);
android.location.LocationListener locationListener = new MyLocationListener();
// getting GPS status
isGPSEnabled = locationManager
.isProviderEnabled(LocationManager.GPS_PROVIDER);
// getting network status
isNetworkEnabled = locationManager
.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (!isGPSEnabled && !isNetworkEnabled) {
// location service disabled
} else {
this.canGetLocation = true;
// if GPS Enabled get lat/long using GPS Servicesif (isGPSEnabled) {
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, locationListener);
Log.d("GPS Enabled", "GPS Enabled");
if (locationManager != null) {
location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
//updateGPSCoordinates();
}
}
// First get location from Network Providerif (isNetworkEnabled) {
if (location == null) {
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, locationListener);
Log.d("Network", "Network");
if (locationManager != null) {
location = locationManager
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
//updateGPSCoordinates();
}
}
}
}
} catch (Exception e) {
// e.printStackTrace();
Log.e("Error : DestinationTo",
"Impossible to connect to LocationManager", e);
}
return location;
}
here mylocationClassListener
privateclassMyLocationListenerimplementsandroid.location.LocationListener {
@Override
public void onLocationChanged(Location loc) {
// do whatever you want
}
@Override
public void onStatusChanged(String s, int i, Bundle bundle) {
}
@Override
public void onProviderEnabled(String s) {
}
@Override
public void onProviderDisabled(String s) {
}
}
And in your main activity you can register your broadcast:
registerReceiver(gpsLocationChangeReciever, newIntentFilter("android.location.PROVIDERS_CHANGED"));
be sure to get locationPermission first
Post a Comment for "Get Location As It Changes"