2011-03-29 10 views
1

ServiceThread을 생성하여 타사에 연결되어 정상적으로 실행됩니다. 그러나 설정으로 이동하여 서비스를 중지하면 계속 실행됩니다. 실행중인 서비스 설정에서 사라지고 onDestroy 메서드가 호출되지만 여전히 호출되는 각 메서드는 Service입니다. 내가 정확히 Thread을 사용하고 있지 않다고 가정하고 있지만, 서비스가 계속 실행되는 이유를 알지 못합니다. 모든 스레드가 완료 될 때까지 계속 실행되지 않는 한.강제 종료 후 Android 서비스가 계속 실행됩니다.

public class DataProcessorService extends Service { 

    private ServiceState _state; 
    private ConnectThread _connectThread; 

    private Handler _handler = new Handler() { 
     public synchronized void handleMessage(Message msg) { 
      stopConnectThread(); 
      onNextState(); // Goes to state after Connecting 
     } 
    }; 

    @Override 
    public void onCreate() { 
     logger.debug("onCreate called"); 

     _state = ServiceState.readState(this); 

     switch (_state) { 
     ... 
     case CONNECTING: 
      onConnecting(); 
      break; 
     ... 
     } 
    } 

    @Override 
    public void onDestroy() { 
     logger.debug("onDestroy called"); 
     stopConnectThread(); 
     ServiceState.saveState(this, _state); 
    } 

    private void onConnecting() { 
     _state = ServiceState.CONNECTING; 
     logger.debug("onConnecting called"); 

     _connectThread = new ConnectThread(this, _handler); 
     _connectThread.setDaemon(true); 
     _connectThread.start(); 
    } 

    private void stopConnectThread() { 
     if (_connectThread != null) { 
      _connectThread.interrupt(); 
      _connectThread = null; 
     } 
    } 
} 

가 여기 내 ConnectThread 클래스의 (나는 또한 주석 섹션입니다 here 제안 된 일을 시도) :

public class ConnectThread extends Thread { 

    private final Context _context; 
    private final Handler _handler; 

// private volatile Thread runner; 

    public ConnectThread(Context context, Handler handler) { 
     super("ConnectThread"); 
     this._context = context; 
     this._handler = handler; 
    } 

// public synchronized void startThread() { 
//  if (runner == null) { 
//   runner = new Thread(this); 
//   runner.start(); 
//  } 
// } 
// 
// public synchronized void stopThread() { 
//  if (runner != null) { 
//   Thread moribund = runner; 
//   runner = null; 
//   moribund.interrupt(); 
//  } 
// } 

    @Override 
    public void run() { 
     Looper.prepare(); 
//  if (Thread.currentThread() == runner) { 
      logger.debug("Service started"); 

      Thread.sleep(5000); //inside try-catch 
//  } 
     Looper.loop(); 
    } 

} 

내가 DDMS에서 보면, 그것은 여러 ConnectThread의 표시를하고 각이야 wait 상태이기 때문에, 나는 그들이 끝나고 죽지 않는다고 가정하고 내 서비스가 멈추는 것을 막을 수 있습니다. 문제가 발생하는 이유를 알거나 해결 방법을 알고 있습니까?

편집 : 나는 어쩌면 어딘가에 Looper.quit() 전화가 필요하다고 생각하기 시작했습니다. LooperHandler에 대해 자세히 알아 보겠습니다. 나는 HandlerThread을보기 시작했으나 아직 Loopers가 무엇인지에 대해서는 분명하지 않습니다. Handler을 내 ConnectThread에게 전달하는 것이 좋지 않습니까?

답변

0

stopConnectThread()에서 루퍼가 루핑하는 것을 중지해야합니다. 스레드를 중단하는 것만으로는 루퍼를 멈추지 않습니다. 이 시도 :

ConnectThread 클래스 추가에

:

ConnectThread.run에서
private Looper myLooper; // A reference to the Looper for this Thread 
public void stopLooper() { 
    // Tell Looper to stop looping 
    myLooper.quit(); 
} 

() 메소드 Looper.prepare() 추가 후 :

myLooper = Looper.myLooper(); // Get the Looper associated with this Thread 
stopConnectThread()에서

대신 _connectThread.interrupt();을 이렇게 :

_connectThread.stopLooper(); 
관련 문제