5

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!

fliX
  • 773
  • 8
  • 24

2 Answers2

10

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.

tir38
  • 9,810
  • 10
  • 64
  • 107
CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • 1
    You might say IntentService is good for synchronous background processing but you'll need to use a Service, or something else, for asynchronous processing. That is, if the thing you're calling has a callback, don't use an IntentService. – lilbyrdie Apr 17 '13 at 14:39
  • so if he wanted to put loop inside his IntentService, that would be a bad practice @CommonsWare? I see, so we should create Service only instead of IntentService right? :D – gumuruh Jul 13 '14 at 06:30
  • @gumuruh: "so if he wanted to put loop inside his IntentService, that would be a bad practice" -- IMHO, yes. Use a regular `Service` with your own background thread, where you can define your own rules for when the service shuts down, what to do if a command is received while the thread is already running, etc. – CommonsWare Jul 13 '14 at 10:49
  • I see, i see. But what make it difference if i use a common thread and binded to a Handler, @CommonsWare ? – gumuruh Jul 14 '14 at 02:57
  • @CommonsWare what is Your percentage usage IntentService vs Service in total projects You made? Do You use in most cases Service? Is it 20/80 ? – deadfish Jan 25 '17 at 11:58
  • @deadfish: Comparing `IntentService` and `Service`, the vast majority are `IntentService`. – CommonsWare Jan 25 '17 at 12:31
0

Consider using a remote service. It worked for me.

Anh3Saigon
  • 199
  • 5
  • I believe remote services run on a separate process, not thread. I tested it using a `Thread.sleep` call in the `Handler` of the remote service, and the entire application hung while the service was sleeping – woojoo666 Jul 02 '14 at 17:37