【发布时间】: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