【问题标题】:Restart Bound Service after force stopped强制停止后重新启动绑定服务
【发布时间】: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


【解决方案1】:

您可以在onDestory() 中编写代码来启动它自己。

祝你好运。

【讨论】:

  • 是的,这是存档目标的肮脏方式。另一种解决方案是编写一个注册到特定事件的广播接收器。就像 BOOT_COMPLETED 一样,但是如果用户杀死,据我所知,只有覆盖 onDestroy 的方法。
  • 可能有一些方法,请检查我们强制停止时WhatsApp重启服务
  • 你能发布你的 onDestory 吗?
  • 我现在没有代码,也许我明天可以发布它,我正在触发来自 onDestroy 的广播。而且我有一个 onDestroy 登录,它没有显示在 logcat 中,这意味着没有调用 Destroy。
猜你喜欢
  • 2013-06-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多