I run the service in the background, which reads the GPS / network location and should do the following:
run in the background without interruptions when you restart the application and save it as much as possible without being killed (this is solved using the comment Merlin below)
in the new place received, call the web service and send the place to read
There is a recurring task running every 60 seconds and resubmitting the last location to the web service. This will help if the user remains in the same position.
There are several things that I have reviewed, and I'm not sure I understood correctly. The service runs in the same thread as the main application, so sending the location to the server in the same thread as the user interface thread can cause the user interface to freeze, which is bad. Also, I'm not sure if the GPS / network listeners have their own streams or they use the same stream as the application.
The following is an abbreviated service code to make it more clear:
public class GPSLoggerService extends Service {
@Override
public void onCreate() {
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 50, locationListenerNetwork);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 50, locationListenerGps);
scheduleTaskExecutor = Executors.newScheduledThreadPool(5);
scheduleTaskExecutor.scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
updateLocation(lastLocation);
}, 60, 60, TimeUnit.SECONDS);
return START_STICKY;
}
LocationListener locationListenerGps = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
updateLocation(location);
}
...
}
LocationListener locationListenerNetwork = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
updateLocation(location);
}
...
}
private void updateLocation(Location readLocation) {
String response = WebServiceCalls.updateLocation(readLocation);
saveLocation(readLocation)
}
, updateLocation . TaskExecutor , . stopService() , TaskExecutor . , .
, : , 60 . .
?