Request Location Updates on Android 12 with Executors

425 Views Asked by At

in our app, I'd like to request the location using the new LocationRequest and the fused location provider. Unfortunately, in the new API I need to pass an Executor to the method call. The executor can either be Context.mainExecutor to run on the main thread, or you need to create your own executor. I could use a `ThreadPoolExecutor, but this sounds a bit like an overkill for this simple use-case.

This is the new API:

public void requestLocationUpdates (String provider, 
                long minTimeMs, 
                float minDistanceM, 
                Executor executor, 
                LocationListener listener)

You can find the documentation here: LocationManager

All the best,
Alex

1

There are 1 best solutions below

0
Jose Antonio Dominguez Garcia On

Executor: the executor handling listener callbacks This value cannot be null. Callback and listener events are dispatched through this Executor, providing an easy way to control which thread is used. To dispatch events through the main thread of your application, you can use Context.getMainExecutor(). Otherwise, provide an Executor that dispatches to an appropriate thread.