强制停止后重新启动绑定服务

时间:2015-11-02 06:40:21

标签: android service android-service

我的应用程序中有一个有限的服务,我正在使用该服务来跟踪用户的位置。我想让这个服务一直在运行,如果用户从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

它停止了。我该如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

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

祝你好运。

相关问题