强制停止后Android服务继续运行

时间:2011-03-29 18:05:35

标签: java android multithreading service

我有Service产生Thread以连接到第三方并且运行正常。但是,如果我转到设置并停止服务,它将继续运行。它在设置中从正在运行的服务中消失,并且调用了onDestroy方法,但我仍然看到Service中的每个方法都被调用。我假设我没有正确使用Thread,但我不明白为什么服务会继续运行...除非它一直运行直到所有线程都完成。

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一个坏主意?

1 个答案:

答案 0 :(得分:0)

你必须停止Looper在stopConnectThread()中循环。只是中断线程不会阻止Looper。试试这个:

在ConnectThread类中添加:

private Looper myLooper; // A reference to the Looper for this Thread
public void stopLooper() {
    // Tell Looper to stop looping
    myLooper.quit();
}
Looper.prepare()添加:

之后,在ConnectThread.run()方法中

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

在stopConnectThread()中,而不是_connectThread.interrupt();执行此操作:

_connectThread.stopLooper();