Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

onLocationChanged callback is made on what thread? The main UI thread?

When this callback is made in my app, I have quite a bit work to do (reading & writing to SQL db through an ORM lib and a number of distance based calculations). Naturally I am concerned about not blocking the main UI thread so I've been trying (unsuccessfully) to find out if this is the thread on which the callback is made. If it is, I'm intending to do all the afore-mentioned work on an AsyncTask triggered when the callback is made. This same AsyncTask will receive events from 2 separate activity classes as well. (Responding to user input etc..)

A lot of the discussion I have found around this callback seems to be based around people trying to change the thread on which the callback is actually received. This makes no sense to me. Surely the platform determines the context of this callback and the sensible thing to do when it's received is offload any serious work onto another thread, for which AsyncTask seems appropriate.

If anyone can outline a successful pattern they've used here it would be really useful.

like image 313
Chris Danson Avatar asked Jun 21 '12 00:06

Chris Danson


People also ask

What is difference between UI thread and main thread?

Originally Answered: What is difference between UI thread and main thread in Android? UI thread is what render UI component/Views. Main thread is what which start the process/app. In Android UI thread is main thread.

What is UI thread in Java?

User Interface Thread or UI-Thread in Android is a Thread element responsible for updating the layout elements of the application implicitly or explicitly. This means, to update an element or change its attributes in the application layout ie the front-end of the application, one can make use of the UI-Thread.

What is onLocationChanged Android?

onLocationChanged(Location location) Called when the location has changed. default void. onProviderDisabled(String provider) Called when the provider this listener is registered with becomes disabled.


1 Answers

According to the Android reference documentation for LocationManager:

The calling thread must be a Looper thread such as the main thread of the calling Activity.

This means that the Thread that initializes the callback must be the main Thread or a Looper Thread.

I've found the best way to handle this is to register an OnLocationChanged receiver on the main Thread. Then, in my callback I'll create a Runnable to send to a background Thread where I'll perform any long-running tasks (like writing to the database).

ExecutorService mThreadPool = Executors.newSingleThreadExecutor();

@Override
public void onLocationChanged(Location location) {
    mThreadPool.execute(new Runnable() {
        @Override
        public void run() {
            // Perform your long-running tasks here.
            //...
        }
    });
}
like image 134
twaddington Avatar answered Oct 16 '22 06:10

twaddington