Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to manage Loopers and Threads (thread doesn't die anymore!)

I created a class extending Thread to retrieve user location through LocationManager in a non-ui thread. I implemented this as a thread because it has to be started on request and do its work just for a limited time. By the way, I had to add a Looper object in the thread, to be able to create the handler for the LocationManager (onLocationChanged).

This is the code:

public class UserLocationThread extends Thread implements LocationListener {
//...
public void run() {
    try {
        Looper.prepare();
        locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this);
        Looper.loop();
        Looper.myLooper().quit();
    } catch (Exception e) {
        //...
    }
}

@Override
public void onLocationChanged(Location location) {
    locationManager.removeUpdates(this);
    //...
    handler.sendMessage(msg); //this is the handler for communication with father thread
}

//...}

I would like the thread to start, receive the user location data (in this case just one time), send the data to the main thread via a message to the handler, and then die. The problem is that in my case the thread does not die anymore, once the run method ended (that should be fine, because otherwise onLocationChanged would not receive the new locations).

But in this way, assuming that thread's stop and suspend methods are deprecated, what would be a good way, in this case at least, to make a thread with a looper die?

Thanks in advance ;)

like image 331
e-cal Avatar asked Jun 08 '11 11:06

e-cal


People also ask

What is a looper thread?

What is Looper? Looper is a class which is used to execute the Messages(Runnables) in a queue. Normal threads have no such queue, e.g. simple thread does not have any queue. It executes once and after method execution finishes, the thread will not run another Message(Runnable).

Is it possible to increase the number of threads in Android?

Technically there is no limit, but at some point having more threads is going to be less efficient than having less. If you want to see a pretty much optimal implementation of a ThreadPool look at the source code of the AsyncTask .

What does Looper prepare do?

Creating Looper and MessageQueue in Thread. Looper. prepare() identifies the calling thread and creates Looper and MessageQueue object and associate the thread with them in LocalThread storage class. Looper. loop() must be called to start the associated looper and Looper.

What is a thread handler Looper and MessageQueue?

It is a kind of worker that serves a MessageQueue for the current thread. Looper loops through a message queue and sends messages to corresponding threads to process. There will be only one unique looper per thread. That means only one MessageQueue per thread. There can be any number of handlers for one single thread.


1 Answers

You can explicitly quit from Looper's loop using Handler:

private Handler mUserLocationHandler = null;
private Handler handler = null;

public class UserLocationThread extends Thread implements LocationListener {    

 public void run() {
    try {
          Looper.prepare();
        mUserLocationHandler = new Handler();
        locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this);
        Looper.loop();

    } catch (Exception e) {
        //...
    }
}


@Override
public void onLocationChanged(Location location) {
    locationManager.removeUpdates(this);
    //...
    handler.sendMessage(msg); 
    if(mUserLocationHandler != null){
        mUserLocationHandler.getLooper().quit();
    }
}
like image 86
inazaruk Avatar answered Nov 15 '22 23:11

inazaruk