I do not explicitly call startService()
在这种情况下,覆盖onStartCommand(并返回START_STICKY)是没有意义的,因为它不会被调用.
1.If I want my Service to run when my Activity is alive and to stop when my Activity get killed, where should I unbind my Service?
如果您不想在onPause中执行此操作,则可以在onDestroy中取消绑定,这很好.在极少数情况下,如果您的活动在没有onDestroy的情况下被杀死,它将被Android解除绑定(因此您的服务也将被解除绑定并且也被正确销毁),如here所述:
When your client is destroyed, it will unbind from the service
至于
2.When the onDestroy() method is currently called and I call
unbindService(), the Service’s onUnbind() method isn’t triggered. Why?
我建议你有其他人绑定它,否则应该调用onUnbind.尝试在此方法中放置一个断点并验证它是否被击中.此外,验证在这种情况下是否调用了服务onDestroy.