Android service and recurring task running in threads

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) {
    //web service call
    String response = WebServiceCalls.updateLocation(readLocation);

    //log data in a local Sqlite database
    saveLocation(readLocation) 
}

, updateLocation . TaskExecutor , . stopService() , TaskExecutor . , . , : , 60 . .

?

+5
4

.

onLocationChanged() asynctask -, .

Edit

  • , requestLocationUpdates. , . . , LocationChanged() . , . , 1 location1 location2 , 1 1.

  • LocationListener GPS NETWORK.

  • onLocationChanged(), .
  • , , GPS , , , webAPI, .
0

IntentService AlarmManager .

, Thread, ,

UPDATE

fooobar.com/questions/322835/...

+4
+2
source

You should use AsynchTask:

public class RefreshTask extends AsyncTask<Integer, Void, Integer> {



    /**
     * The system calls this to perform work in a worker thread and delivers
     * it the parameters given to AsyncTask.execute()
     */

    public RefreshTask() {

    }

    protected Integer doInBackground(Integer... millis) {
        try{
            int waitTime = 0;
            while(waitTime<60000){
                Thread.sleep(100);
                waitTime += 100;
            }

            //update location here

        }catch(InterruptedException e){

        }



        return 1;
    }

    /**
     * The system calls this to perform work in the UI thread and delivers
     * the result from doInBackground()
     */
    protected void onPostExecute(Integer result) {
        new RefreshTask.execute();
    }


}
-1
source

All Articles