从线程内停止 Android 服务
我有一个服务在 onCreate()
期间启动一个线程。该线程正在监视硬件(IOIO),一旦断开连接就会结束,因此本质上是一个无限循环。从 Activity 停止服务工作正常,线程也会停止(以 onDestory()
结束)。
我的问题是,如果线程死亡(比如由于异常),是否可以停止生成它的服务。
我尝试在线程代码中使用 stopSelf()
但服务不会停止。
谢谢 ps 我的第一篇文章,所以请原谅我应该遵循的任何遗漏约定。
I have a Service which starts a thread during onCreate()
. The thread is monitoring hardware (IOIO) and will end once it is disconnected so essesentially is an infinite loop. Stopping the service from an Activity works fine and the thread also stops (ended in onDestory()
).
My problem is that if the thread dies (say because of an exception) is it possible to stop the service that spawned it.
I've tried using stopSelf()
from within the thread code but the service does not stop.
Thank you
ps my first post so please excuse any missing conventions I should have followed.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
存储服务的Intent并直接停止怎么样?
在服务中:
在服务内的线程中:
How about storing the Intent of the service and stopping it directly?
In the service:
In the thread inside the service:
我认为您在这里可能想要使用 Handler。本质上,为您的 Sevrice 创建一个处理程序(确保它是最终的,以便您可以在所有线程中使用)。然后在另一个线程中调用:
I think what you want here might be to use a Handler. Eseentially, create a handler for your Sevrice (make sure it's final so you can use across all threads). Then in your other thread, call: