0

i have a service that receives location updates every 1-3 seconds or so. the service is started from the main activity using a pending intent:

    mFusedLocationClientFast.requestLocationUpdates(mLocationRequest, getPendingIntent());

where getPendingIntent() is a method that defines an action for my IntentService to handle in handleIntent()

however, a strange thing i noticed is that every time the IntentService handles a location update in handleIntent(), it's always immediately preceded by a call to onStartCommand() of the same service.

does anyone know why onStartCommand() needs to be called every time the service handles an intent? thanks

Russell Butler
  • 326
  • 4
  • 15

1 Answers1

1

An IntentService is, simply, a Service that runs onHandleIntent on a worker thread. The mechanism by which a Service receives an intent is onStartCommand.

In an IntentService, the onStartCommand method forwards the intent to the onHandleIntent method.

The code is here

It is puzzling, to me, that the designers of the IntentService did not make its implementations of onStartCommand and onStart final

G. Blake Meike
  • 6,615
  • 3
  • 24
  • 40
  • got it...so every time onHandleIntent runs to completion, the service is stopped and gets recreated everytime a new location comes in. i guess IntentService probably isn't the best mechanism for this task...i'm trying to make something similar to a run tracker, where you can dynamically view your location throughout the day. – Russell Butler Dec 24 '19 at 17:05