我的 android APP 上有一个后台服务,它正在获取我的 GPS 位置并将其发送到远程数据库。它工作正常。
问题是当我想停止服务时......它不会停止:S。 logcat 上也没有出现异常或错误......它根本不会停止。
这是启动我的服务的代码(带有按钮):
startService(new Intent(GPSLoc.this, MyService.class)); //enciendo el service
这是我停止它的代码(在 onactivityresult 方法上):
stopService(new Intent(GPSLoc.this, MyService.class));
我已经调试了这个应用程序,并且我检查了每次调试它时都调用了 stopService 代码行,但它并没有停止......
我确定它没有停止,因为当我按下按钮停止服务时,我仍然从模拟器接收 gps 位置。
我做错了什么?
最佳答案
你实现了 onDestroy()
了吗?如果没有,我相信这可能是解决方案 - 你停止你的 Timer
或任何你用来在 onDestroy()
中运行服务的东西。
A service can be stopped by calling its stopSelf() method, or by calling Context.stopService().
查看 link了解更多信息。
关于android - stopService 不会停止我的服务.... 为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4470418/