If you use BundleContext, you will get just an instantaneous snapshot of the current services; you will not hear about any services registered or unregistered after you got the snapshot.
If you use ServiceListener, you will only get events for services registered or unregistered after you create the listener, but you will miss the services that were already registered before that. So if you use this low-level API, you need to both query BundleContext *and* create a ServiceListener, and you need to do it very carefully because of the potential for race conditions. This is in fact quite complicated and difficult. ServiceTracker exists to make it much easier, because it notifies you of services registered before you opened the tracker, *and* services that are registered later on. Therefore I strongly recommend ServiceTracker over the lower level context query + ServiceListener approach. However there is a higher level approach than even ServiceTracker, called Declarative Services. This allow you to simply declare a field in your class and have the service injected automatically. Always use the highest level abstraction that you can get away with, and only drop down to the lower levels if you need to do something unusual that is not supported by the abstraction. BTW you should not generally store a service in a member variable because that service might go away very shortly afterwards. You should listen to the service events and release your reference to the service when asked to. ServiceTracker handles this for you; it's kind of a "smart pointer". Neil On Fri, Feb 15, 2013 at 9:55 AM, Alexandre Ribeiro <[email protected]>wrote: > Hello > > I would like to understand the difference between getting the service using > the context (start method) or by using the servicetracker. > > By the way, servicelistener is usefull to be notified about changes in the > services and to avoid pulling for them. But what about service tracker? > > > Using context we can save it in a member variable and use it later to get > services. It seems that it makes the same of servicetracker. Could you > highlight me the real differences between using context and servicetracker > to get service references? > > > > Thank you >

