如何从一个弯针退出?

时间:2012-10-05 11:57:31

标签: android android-asynctask

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

我正在调用Thread中的AsyncTask班级。当我使用1-6中的代码调用它时,它会创建Thread并运行它。但AsyncTask被卡在那里。我需要在不阻止Thread的情况下运行其他AsyncTask。如何实现?

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);
}                   

2 个答案:

答案 0 :(得分:0)

你需要调用Looper.quit();无论如何,它永远不会退出。在你需要调用它的地方我不知道,除非你发布更多代码:)

答案 1 :(得分:0)

与文档相反,并不总是需要退出活套 如果你坚持 - 你必须在弯针的同一个线程上调用它 请参阅我精心回答的here