I have an installed bundle. Now I want to add listener to it (and later to start) to find when it comes to active state. The only way I found is
bundle.getBundleContext().addBundleListener(new ...);
However getBundleContext() works only if bundle is in starting, stopping, active state. So, can can I do that?
It does not make sense to register a BundleListener to catch events that happen with the same bundle. When you implement a BundleListener / BundleTracker, you normally want to catch the events of bundles with special attributes.
With a BundleListener you can catch the events that happen in the framework. With a BundleTracker, you first catch the last events that happened every bundle in the framework than you can catch the new events. Often it is better to use a BundleTracker as you want to pick up bundles with those special attributes that are already active.
The BundleListener / BundleTracker should be used with the help of the BundleContext of the bundle that implements the listener / tracker. As code should not run in your bundle before it is "starting", the BundleContext should be always available when you want to register the listener / tracker.
It would be useful to know more about the use-case that you wanted to implement. Maybe you do not even need to implement a BundleListener / BundleTracker at all, just re-design the code in your bundle a bit.