问题描述
我建了一个服务,并预期它没有工作,所以我调试了。在调试过程中,我看到了服务的的onDestroy()方法是从的onCreate回国后直接调用()。当我注释掉发生在的onDestroy(清理),服务做的工作,但是这不应该是我的解决办法。所以我的问题是,为什么是的onDestroy()这么早打来电话,为什么服务呢运行?或者,我怎么能prevent的onDestroy()被调用,在错误的时间?
I built a Service and it didn't work as expected, so I debugged it. During debugging, I saw that the service's onDestroy() method is called directly after returning from onCreate(). When I comment out the cleanup that happens in onDestroy(), the service does its work, but this should not be my solution. So my question is, why is onDestroy() called so early and why is the service running anyway? Or how can I prevent onDestroy() from being called at the wrong time?
有关您的信息:我的子类IntentService
For your information: I've subclassed IntentService.
感谢您的帮助。
Binabik
推荐答案
如果你是子类IntentService你应该使用onHandleIntent(Intent意图)为您服务的生命周期。您的服务可能会被转移到快速的onDestroy监守你没有code onHandleIntent内。虽然没有你的code,我不能肯定地说。
If you are subclassing IntentService you should be using onHandleIntent(Intent intent) for the lifecycle of your service. Your service might be moving to onDestroy quickly becuase you do not have code inside of onHandleIntent. Although without your code I cannot say for sure.
此外,它可能会跳投移动到的onDestroy很快,因为IntentService是自动螺纹为你可能只是启动工作线程这就要求onHandleIntent并移动到的onDestroy。
Also it might aways move to onDestroy quickly because IntentService is auto threaded for you and might just launch the worker thread which calls onHandleIntent and move to onDestroy.
这篇关于Service.onDestroy()被调用创建之后直接,反正服务做的工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!