4

应用程序和后台服务一起运行。但是当应用程序关闭时,后台服务正在重新启动?
为什么?如何阻止此重新启动?

当应用程序关闭时,服务应该像什么都没发生一样继续。我该怎么做?



我在 MainActivity 中使用此代码:

尝试连接();

private boolean isMyServiceRunning(Class<?> serviceClass) {
    ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
    for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
        if (serviceClass.getName().equals(service.service.getClassName())) {
            return true;
        }
    }
    return false;
}
private void tryConnect(){
    if(!isMyServiceRunning(BackService.class)){
        startService(new Intent(this,BackService.class));
    }
}

后台服务返回 START_STICKY;(我试过START_NOT_STICKY,START_REDELIVER_INTENT,不起作用)
我在android应用程序中没有onDestroy。

4

1 回答 1

0

服务和活动是否在同一个进程上运行?如果是这样,那么这可能就是您的服务行为如此的原因(如果两者共享相同的进程,那么当应用程序终止进程时也会终止,如果它使用 START_STICKY 会导致服务自行重启)......也许您应该尝试运行该服务在一个单独的过程中..

于 2015-03-12T13:22:26.487 回答