android - stopService 不会停止我的服务.... 为什么?

标签 android service

我的 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/

相关文章:

java.lang.ClassCastException : android. os.BinderProxy 无法转换为 xxx$LocalBinder

java - 客户端中的 Jersey 响应正文为空

android - 无一异常(exception)地寻找解决方案

android - Dagger2 + Kotlin : lateinit property has not been initialized

android - 如何为 Android 创建 DFP 原生广告单元 ID

c# - .NET Windows 服务中的消息泵

javascript - 使用 ES6 语法注册 AngularJS 服务

javascript - meteor : Cordova 和网站的不同(铁)路线

java - RemoteView 中的动态 GradientDrawable

android - 已 Root 的 Android 设备抛出 EACCES : permission denied exception when I try to get CPU's temperature