Android 已启动和绑定的服务被神秘地停止和重新启动

Android 已启动和绑定的服务被神秘地停止和重新启动,android,android-service,Android,Android Service,我的活动尝试启动并绑定一个应该独立运行的服务。这项服务将打开GPS onCreate调用getApplicationContext.StartService,onResume调用getApplicationContext.BindService。OnPause调用getApplicationContext.unbindService,尽管它似乎从未正常运行(服务连接从未记录解除绑定,尽管在我以类似方式处理绑定时会记录绑定) 不幸的是,当我打开最近的列表,将活动滑开时,服务会停止,然后几乎立即重新

我的活动尝试启动并绑定一个应该独立运行的服务。这项服务将打开GPS

onCreate调用
getApplicationContext.StartService
,onResume调用
getApplicationContext.BindService
。OnPause调用
getApplicationContext.unbindService
,尽管它似乎从未正常运行(服务连接从未记录解除绑定,尽管在我以类似方式处理绑定时会记录绑定)

不幸的是,当我打开最近的列表,将活动滑开时,服务会停止,然后几乎立即重新启动,GPS连接会断开。什么会导致这种行为

@Override
protected void onStart() {
    super.onStart();
}

@Override
protected void onResume() {
    super.onResume();

    // Start up the service
    Intent intent = new Intent(getApplicationContext(), LockService.class);
    getApplicationContext().bindService(intent, myConnection, Context.BIND_AUTO_CREATE);
}

@Override
protected void onPause() {
    super.onPause();
    Log.i(tag, "onPause");
    if (isBound) {
        getApplicationContext().unbindService(myConnection);
    }
}

编辑:我检查了这个答案,但这只会阻止服务立即重新启动(它仍然挂起,重新打开活动会重新初始化它):

编辑2:这个答案让我的希望有点过分了。它似乎也不能解决任何问题

编辑3:这可能是一件很无聊的事情


因为在Android版本4.4(KK)中,将应用程序从最近事件列表中删除会导致其死亡,所以我选择根本不在最近事件列表中显示我的应用程序

哦,反正它也不需要住在那里。它很高兴地存在于通知栏中。我很同情那些不那么幸运的人,他们需要通过定时器和一些陈腐的代码强制重启服务

// Bound service stuff
    LockService myService;
    boolean isBound = false;

    private ServiceConnection myConnection = new ServiceConnection() {

    @Override
    public void onServiceConnected(ComponentName className, IBinder service) {
        LockBinder binder = (LockBinder) service;
        myService = binder.getService();
        isBound = true;

        final boolean status = myService.getStatus();
        Log.i(tag, "service bound");
    }

    @Override
    public void onServiceDisconnected(ComponentName className) {
        isBound = false;
        Log.i(tag, "service unbound");
    }
};