stopService 不会停止我的服务.... 为什么? [英] stopService doesn't stop's my service.... why?
问题描述
我的 android APP 上有一个后台服务,可以获取我的 GPS 位置并将其发送到远程数据库.它工作正常.
i have a background service on my android APP that is getting my GPS position and sending it to a remote db. It work's fine.
问题是当我想停止服务时......它不会停止:S.logcat 上也没有出现异常或错误......它根本没有停止.
The problem is when i want to stop the service.... it doesn't stops :S. Also no exception or errors on logcat have appeared... it simply doesn't stops.
这是启动我的服务的代码(带按钮):
this is the code to start my srvice (with a button):
startService(new Intent(GPSLoc.this, MyService.class)); //enciendo el service
这是我停止它的代码(在 onactivityresult 方法上):
this is the code where I stop it (on the onactivityresult method):
stopService(new Intent(GPSLoc.this, MyService.class));
我已经调试了应用程序,我检查了每次调试它时都调用了 stopService 代码行,但它并没有停止......
I have been debugged the app, and i checked that the stopService codeline has been called every time that i debugged it, but it doesn't stops......
我确信它没有停止,因为在我的数据库上,当我按下按钮停止服务时,我仍然从模拟器接收 GPS 位置.
i am sure that it's not stopped cause on my database i still recive gps positions from the emulator when i have press the button to stop the service.
我做错了什么?
推荐答案
您是否实施了 onDestroy()
?如果没有,我相信这可能是解决方案 - 您停止 Timer
或用于在 onDestroy()
中运行服务的任何东西.
Have you implemented onDestroy()
? If not, I believe that might be the solution - and you stop your Timer
or whatever you're using to run the service within onDestroy()
.
可以通过调用其 stopSelf() 方法或通过调用 Context.stopService() 来停止服务.
A service can be stopped by calling its stopSelf() method, or by calling Context.stopService().
有关更多信息,请参阅此链接.
See this link for some more information.
这篇关于stopService 不会停止我的服务.... 为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!