I have an app that has a client Activity and 开发者_JS百科a long-running Service. The Service may be started by the alarm manager or by the Activity itself.
The alarm intent calls startService(). The Activity calls startService, and then binds itself to the service so it can get information from it.
When I back-button out of the Activity, onDestroy() is called on the Activity, the Activity unbinds from the Service, and onDestroy() is called on the Service.
I was under the impression that if I called startService(), then the Service would stay around until stopService()/stopSelf() was called, no matter what clients have unbound from it.
Am I misunderstanding something?
It partly depends on the SDK version you are using. In this Service Lifecycle reference, it says onStartCommand() must return START_STICKY for the behavior you want. Prior to API level 5, there was no onStartCommand and services were all sticky.
精彩评论