0
1. Looper.prepare();
2. Handler mHandler = new Handler() {
3. public void handleMessage(Message msg) {}
4. };
5. mHandler.post(gpsLocationListenerThread);
6. Looper.loop();
7. 

I'm calling a Thread class from AsyncTask. When i call it using code from 1-6, it creates the Thread and runs it. But AsyncTask get stucked there. I need to run this other Thread without blocking my AsyncTask. How to make it happen?

public GPSLocationListenerThread(Context context){
    this.context = context;
    mlocManager = (LocationManager)context.getSystemService(context.LOCATION_SERVICE);
    mlocListener = new GPSLocationListener();
}

public void setHandler(Handler _h){
    this.mHandler = _h;
}

public void run(){
    mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 10000, 100, mlocListener); // in 1000 mseconds or in 100m change
    mlocManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 10000, 100, mlocListener); // in 1000 mseconds or in 100m change
    //mHandler.getLooper().quit();
    while (DataHolder.getDataHolderObject().isTripStarted()){
        try {
            this.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    mlocManager.removeUpdates(mlocListener);
}                   
dinesh707
  • 12,106
  • 22
  • 84
  • 134

2 Answers2

0

Well you need to call Looper.quit(); anyway or it will never quit. Where you need to call it i have no idea unless you post a bit more code :)

0

Contrary to the documentation, it's not always needed to quit a looper.
And if you insist - you must call it on the same thread of the looper.
See my elaborate answer here.

Community
  • 1
  • 1
Amir Uval
  • 14,425
  • 4
  • 50
  • 74