I am doing a "Tracking" app or follow-up, to get the location I use LocationListener within a service, I run the app on the device but sometimes I can notice that the location is somewhat erratic and even sometimes does not receive updates (the coordinates are kept at zero), I've been reading about other methods to obtain the location (FusedLocationProvider), I would like to know if I can do this with LocationListener and the best way to do it or if it is more convenient to use FusedLocationProvider as an alternative. p>
This is the code of my service:
public class LocationService extends Service implements LocationListener {
private final String DEBUG_TAG = "GPSPING";
private LocationManager lm;
private double latitude;
private double longitude;
private double accuracy;
private Location location;
private boolean isEnabled = false;
@Override
public void onLocationChanged(Location location) {
Log.d(DEBUG_TAG, "onLocationChanged");
latitude = location.getLatitude();
longitude = location.getLongitude();
accuracy = location.getAccuracy();
}
@Override
public void onProviderDisabled(String provider) {
Log.d(DEBUG_TAG, "onProviderDisabled");
}
@Override
public void onProviderEnabled(String provider) {
Log.d(DEBUG_TAG, "onProviderEnabled");
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 10f, this);
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
Log.d(DEBUG_TAG, "onStatusChanged");
}
@Override
public void onCreate() {
Log.d(DEBUG_TAG, "onCreate");
}
@Override
public void onDestroy() {
lm.removeUpdates(this);
Log.d(DEBUG_TAG, "onDestroy");
}
@Override
public IBinder onBind(Intent intent) {
Log.d(DEBUG_TAG, "onBind");
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onStart(final Intent intent, int startid) {
Log.d(DEBUG_TAG, "onStart");
Location location = getLocation();
latitude = location.getLatitude();
longitude = location.getLongitude();
Log.d(DEBUG_TAG, "Location Lat:"+latitude +"Long: "+longitude);
}
public Location getLocation() {
try {
lm = (LocationManager) this.getSystemService(LOCATION_SERVICE);
boolean isGPSEnabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
boolean isNetworkEnabled = lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (!isGPSEnabled && !isNetworkEnabled) {
// no network provider is enabled
} else {
if (isNetworkEnabled) {
lm.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER,
1000,
10f, this);
Log.d("Network", "Network Enabled");
if (lm != null) {
location = lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
if (isGPSEnabled) {
if (location == null) {
lm.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
1000,
10f, this);
Log.d("GPS", "GPS Enabled");
if (lm != null) {
location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return location;
}
}