【问题标题】:Thread servicestartarguments still running after stopService线程 servicestartarguments 在 stopService 之后仍在运行
【发布时间】:2016-08-05 05:47:22
【问题描述】:

在我的 Android 应用程序中,我通过此示例代码运行服务:

startService(new Intent(this, TaxiService.class)); 

之后,有两个线程启动,一个作为服务,第二个作为 ServiceStartArguments,然后我通过以下方式停止服务:

stopService(new Intent(this, TaxiService.class));

在 stopService 之后,我仍然看到名为 ServiceStartArguments 的线程。 如果我运行并停止第 n 个服务,我会看到更多名为 ServiceStartArguments 的线程。 为什么以及如何使用 ServiceStartArguments 停止线程? 谢谢。

已编辑 我发现了问题,但没有解决方案。在我的服务中,我盯着这样的线程:

thread = new HandlerThread("ServiceStartArguments", android.os.Process.THREAD_PRIORITY_BACKGROUND);
thread.start();
mServiceLooper = thread.getLooper();

在 Destroy 方法中我有:

mServiceHandler.removeCallbacks(thread);        
super.onDestroy();
_started = false;

但线程仍在运行。

这是 ServiceStartArguments 线程的一部分:

            _started = true;
            int seven = 0;
            while (_started) {              
                try {
                    Thread.sleep(1000);
...

【问题讨论】:

    标签: java android multithreading service


    【解决方案1】:

    最后我找到了解决方案。我忘了在 Destroy 方法中添加这一行:

    mServiceLooper.quit();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-07-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多