IntentService: how to register correctly?

In my code, I use IntentServiceto listen for location updates (GPS or network updates), and this one IntentServicefires when the event is received, so it fires startService()from any activity,

public class AddLocationService extends IntentService implements LocationListener {
    /*My code here*/
}

    @Override
protected void onHandleIntent(Intent intent) {
    if(getOldLoc() == null) 
    { 
        //Get a new location
        this.locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, TIME_INTERVAL_GPS, 0, this);
        this.locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, TIME_INTERVAL_GPS, 0, this);
        Log.d(AddLocationService.TAG, "Network listener started");

        this.time_start_listening = System.currentTimeMillis();
        mTimerThread mTimerRunnable = new mTimerThread();
        this.timerThread = new Thread(mTimerRunnable);
        this.timerThread.start();
    }
    else    
        /*REUSE OLD LOCATION*/
}

Now my problem is: when two events fire this one IntentService, and the second fires it while the first one is still requesting updates, I like the second one to wait until the first one is completely completed (found location found OR thread). However, whenever it IntentServiceruns a second time (the first instance is still running), it prints a log to me and executes it as it was running in parallel.

, IntentService , - , , ...

-?

+5
3

, onHandleIntent , , . , LocationManager , , , , onHandleIntent .

IntentService , IntentService , .

, :

public class TestService extends IntentService {
    private static final String TAG = "TestService";

    private Location mLocation = null;

    public TestService() {
       super(TAG);
    }

    @Override
    public void onHandleIntent(Intent intent) {
        Log.d(TAG, "onHandleIntent");

        if (mLocation == null) {
            Log.d(TAG, "launching location thread");
            LocationManager locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);

            LocationThread thread = new LocationThread(locationManager);
            thread.start();
            try {
                thread.join(10000);
            } catch (InterruptedException e) {
                Log.d(TAG, "timeout");
                return;
            }

            Log.d(TAG, "join finished, loc="+mLocation.toString());
        } else {
             Log.d(TAG, "using existing loc="+mLocation.toString());
        }
    }

    private class LocationThread extends Thread implements LocationListener  {
        private LocationManager locationManager = null;

        public LocationThread(LocationManager locationManager) {
            super("UploaderService-Uploader");
            this.locationManager = locationManager;
        }

        @Override
        public void run() {
            Log.d(TAG, "Thread.run");
            Looper.prepare();
           this.locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this);
            this.locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);

            Looper.loop();
        }

        @Override
        public void onLocationChanged(Location location) {
            // TODO Auto-generated method stub
            Log.d(TAG, "onLocationChanged("+location.toString()+")");
            mLocation = location;
            Looper.myLooper().quit();
         }

         @Override
         public void onProviderDisabled(String arg0) {
         }

         @Override
         public void onProviderEnabled(String arg0) {
         }

         @Override
         public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
         }

   }
}

Looper, ( ).

, IntentService, .

+3

onHandleIntent . ( ) . IntellService .

+2

, , , . , , !

, - , , ( join(millis)), onHandleIntent():

                if(thread.isAlive())
                {
                    thread.onThreadStop();
                    try{
                        thread.interrupt();
                    }catch (Exception e) {
                        Log.d(TAG, "Exception on interrupt: " + e.getMessage());
                    }
                }   

thread.join(yourTime), , , , . onThreadStop():

                /*We remove location updates here and stop the looper*/
                public void onThreadStop()
                {
                    this.locationManager1.removeUpdates(this);
                    handleLocationChange(AddLocationService.this.currentBestLocation);
                    Looper.myLooper().quit();
                }

However, I thought I saw how my two intentions are processed the first time I run this code, but now only the first is processed when I have several intentions, still requesting location updates. My method onHandleIntent()seems to execute correctly, stops the thread after the specified time, and even displays the most recent log (the last statement of the method), but the second intention is not executed ... Think about why?

+1
source

All Articles