How do I keep the Thread of an IntentService alive?

fliX picture fliX · Jul 28, 2012 · Viewed 7.9k times · Source

I'm writing a test application which keeps track of the users location. The idea is that i can start a service which then registers for location updates. For now I'm using an IntentService for that.

The service code (which is not working...) looks like that:

public class GpsGatheringService extends IntentService {

// vars
private static final String TAG = "GpsGatheringService";
private boolean stopThread;

// constructors
public GpsGatheringService() {
    super("GpsGatheringServiceThread");
    stopThread = false;
}

// methods
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    Log.d(TAG, "onStartCommand()");
    return super.onStartCommand(intent, flags, startId);
}

@Override
protected void onHandleIntent(Intent arg0) {
    // this is running in a dedicated thread
    Log.d(TAG, "onHandleIntent()");
    LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
    LocationListener locationListener = new LocationListener() {

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {
            Log.d(TAG, "onStatusChanged()");
        }

        @Override
        public void onProviderEnabled(String provider) {
            Log.d(TAG, "onProviderEnabled()");
        }

        @Override
        public void onProviderDisabled(String provider) {
            Log.d(TAG, "onProviderDisabled()");
        }

        @Override
        public void onLocationChanged(Location location) {
            Log.d(TAG, "onLocationChanged()");
        }
    };
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);

    while (!stopThread) {
        try {
            Log.d(TAG, "Going to sleep...");
            Thread.sleep(1500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

@Override
public void onDestroy() {
    super.onDestroy();
    Log.d(TAG, "onDestroy()");
    stopThread = true;
}

}

Currently the only thing which happens is the output of "Going to sleep...". I need some mechanism which keeps the thread alive (because else the listener is not reachable anymore for status updates) and doesnt waste cpu time (I think busy looping is not the preferred way). Even if there are tons of other ways how to realize the application behaviour (logging of gps coordinates) I am interested in a solution to this way to learn a technique to solve problems of this nature!

Answer

CommonsWare picture CommonsWare · Jul 28, 2012

How do I keep the Thread of an IntentService alive?

You don't. You do not use IntentService in a scenario like this.

I need some mechanism which keeps the thread alive (because else the listener is not reachable anymore for status updates) and doesnt waste cpu time (I think busy looping is not the preferred way)

No, in this case a Service is fine, because you control when the service is going away, you control the lifetime of any threads you create, etc. IntentService is unsuitable for your intended purpose, because it controls when the service is going away and it controls the lifetime of the threads.