Search code examples
androidandroid-locationandroid-handler

Android Started Service, Handler Thread with Location Services Api


In my app scenario, I want to keep Started Service throughout the life of my app. In this service, I have to use FusedLocationProviderClient with LocationServices api to request location updates (for exactly, in onStartCommand method). Whenever the new "location update" received, the updated location will be saved into the PreferenceManager.

When the user logouts from the app, I will stop this Started Service.

Do I need to handle this location update logic in new HandlerThread (you know, because Started Service runs on Main Thread)?

From my opinion, since the FusedLocationProviderClient.requestLocationUpdates (from LocationServices Api) method is non-blocking call and based on Asynchronous programming model, I think HandlerThread will not be needed in my scenario.

P.S. I took the references from GoogleSample. In that sample, they demonstrate the Bound Service which is also started (will not be ended when not client bound). They also don't implement the actual logic for HandlerThread to get the location updates.


Solution

  • It depends on what you do when a "new location" event fires.

    Do you do heavy processing or networks calls? -> separate thread. Do you do simple stuff that only takes a couple ms? -> doesn't really matter which of these threads you run it on.