【发布时间】:2015-11-03 13:13:28
【问题描述】:
我的应用程序中有一个有界服务,我正在使用该服务来跟踪用户的位置。我想让该服务始终运行,如果用户从 android 应用程序设置中终止该服务,或使用任何任务杀手应用程序,该服务将停止并且不会重新启动。所以我希望这项服务作为一项高优先级服务。
onStartCommand的服务如下
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
LogUtils.LOGI(TAG, "Service onStartCommand called " + startId);
startLocationUpdates();
return super.onStartCommand(intent, flags, startId);
}
如果返回值为super.onStartCommand(intent, flags, startId);会是什么行为
我已将返回值更改为START_STICKY,但它仍然没有重述。我要做什么改变?
编辑
我正在从服务的 onDestroy() 方法重新启动服务。它工作正常。但如果我立即强制关闭两次,则服务的 onDestroy 不会调用。所以我无法重启
检查我的日志:
11-03 10:36:26.455/tag: ACTION_SERVICE_FORCE_CLOSE on receive called
11-03 10:36:26.486/LocationUpdateService: Service created
11-03 10:36:26.486/LocationUpdateService: Building GoogleApiClient
11-03 10:36:26.486/LocationUpdateService: Service onStartCommand called 1
11-03 10:36:26.525/LocationUpdateService: GoogleApiClient callback onConnected called
11-03 10:36:30.134/LocationUpdateService: Service destroyed
11-03 10:36:30.361/tag: ACTION_SERVICE_FORCE_CLOSE on receive called
11-03 10:36:30.369/LocationUpdateService: Service created
11-03 10:36:30.369/LocationUpdateService: Building GoogleApiClient
11-03 10:36:30.376/LocationUpdateService: Service onStartCommand called 1
11-03 10:36:30.415/LocationUpdateService: GoogleApiClient callback onConnected called
它停在那里。我该如何解决这个问题?
【问题讨论】:
-
你试过'START_REDELIVER_INTENT'吗?
-
不使用任务杀手应用程序或不使用“强制停止”?或两者? [我希望它可以与任务杀手一起使用,因为据说它们就像系统自己的“内存不足杀手”一样工作]
-
强制停止时不工作
-
如果我用任务杀手应用程序杀死,则重新启动(工作正常)
-
区别似乎是任务杀手应用程序调用'ActivityManager.killBackgroundProcesses()',就像系统在内存不足时所做的那样。您可以在服务中使用“startForeground()”以确保安全。 - “用户强制停止”的工作方式似乎有所不同。非常。但随后会警告用户,如果应用被强制停止,应用数据(及其行为)可能会损坏。
标签: android service android-service