This example creates a simple bundle that listens for OSGi service events. This example does not do much at first, because it only prints out the details of registering and unregistering services. In the next example we will create a bundle that implements a service, which will cause this bundle to actually do something. For now, we will just use this example to help us understand the basics of creating a bundle.
A bundle gains access to the OSGi framework using a unique instance of BundleContext. In order for a bundle to get its unique bundle context, it must implement the BundleActivator interface; this interface has two methods, start() and stop(), that both receive the bundle's context and are called when the bundle is started and stopped, respectively. In the following source code, our bundle implements the BundleActivator interface and uses the context to add itself as a listener for service events (the file for our bundle is called Activator.java):
/*
* Apache Felix OSGi tutorial.
**/
package tutorial.example1;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceListener;
import org.osgi.framework.ServiceEvent;
/**
* This class implements a simple bundle that utilizes the OSGi
* framework's event mechanism to listen for service events. Upon
* receiving a service event, it prints out the event's details.
**/
public class Activator implements BundleActivator, ServiceListener
{
/**
* Implements BundleActivator.start(). Prints
* a message and adds itself to the bundle context as a service
* listener.
* @param context the framework context for the bundle.
**/
public void start(BundleContext context)
{
System.out.println("Starting to listen for service events.");
context.addServiceListener(this);
}
/**
* Implements BundleActivator.stop(). Prints
* a message and removes itself from the bundle context as a
* service listener.
* @param context the framework context for the bundle.
**/
public void stop(BundleContext context)
{
context.removeServiceListener(this);
System.out.println("Stopped listening for service events.");
}
/**
* Implements ServiceListener.serviceChanged().
* Prints the details of any service event from the framework.
* @param event the fired service event.
**/
public void serviceChanged(ServiceEvent event)
{
String[] objectClass = (String[])
event.getServiceReference().getProperty("objectClass");
if (event.getType() == ServiceEvent.REGISTERED)
{
System.out.println(
"Ex1: Service of type " + objectClass[0] + " registered.");
}
else if (event.getType() == ServiceEvent.UNREGISTERING)
{
System.out.println(
"Ex1: Service of type " + objectClass[0] + " unregistered.");
}
else if (event.getType() == ServiceEvent.MODIFIED)
{
System.out.println(
"Ex1: Service of type " + objectClass[0] + " modified.");
}
}
}
After implementing the Java source code for the bundle, we must also define a manifest file that contains meta-data needed by the OSGi framework for manipulating the bundle. The manifest is packaged into a JAR file along with the Java class file associated with our bundle; the whole JAR package is actually referred to as a bundle. We create a file called manifest.mf that contains the following:
Bundle-Name: Service listener example
Bundle-Description: A bundle that displays messages at startup and when service events occur
Bundle-Vendor: Apache Felix
Bundle-Version: 1.0.0
Bundle-Activator: tutorial.example1.Activator
Import-Package: org.osgi.framework
分享到:
相关推荐
**OSGI Bundle Change Listener** OSGi(Open Service Gateway Initiative)是一种Java模块化系统,它允许在运行时动态地发现、加载、卸载和更新服务。OSGi的核心是其框架,它管理一组称为bundle的模块。每个bundle...
4. `Service Listener`: 监听服务注册表,当服务发生变化时,触发相应的回调方法。 5. `Service Event`: 包含了服务变化的信息,如服务注册、更改或注销。 总的来说,OSGI服务DS事件是OSGI框架中实现组件间动态交互...
标题中的“基于gemini的blueprint(原生是Spring DM)实现对bundle生命周期的监听”涉及到的是OSGi(Open Service Gateway Initiative)框架下的一种服务管理机制。OSGi是一种模块化系统,它允许Java应用程序被分解为...
public class EventListener { @EventHandler public void handleEvent(Event event) { // 事件处理逻辑 } } ``` #### 九、OSGi与Spring框架的整合 OSGi可以与Spring框架整合,利用Spring的强大功能进行依赖...
GpsStatus.Listener l = new GpsStatus.Listener() { public void onGpsStatusChanged(int event) { switch (event) { case GpsStatus.GPS_EVENT_STARTED: Log.i(TAG, "GPS 启动"); break; case GpsStatus.GPS...
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(editText.getWindowToken(), 0); } return super.onTouchEvent(event); } } ``...
private final GpsStatus.Listener statusListener = new GpsStatus.Listener() { @Override public void onGpsStatusChanged(int event) { LocationManager locationManager = (LocationManager) ...
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); sensorMgr = (SensorManager) getSystemService(SENSOR_SERVICE); sensor = sensorMgr.getDefaultSensor(Sensor....
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); light = new TextView(this); setContentView(light); sensorManager = (SensorManager) getSystemService...
manager = (SensorManager) getSystemService(Context.SENSOR_SERVICE); } @Override protected void onResume() { Sensor sensor = manager.getDefaultSensor(Sensor.TYPE_ORIENTATION); manager....
- `onTouchEvent(MotionEvent event)`: 处理触摸事件。 - **Android动画基础** - Android支持多种类型的动画,如属性动画、帧动画等。 - **关键类**: - `ValueAnimator`: 实现属性动画的核心类。 - `...
manager = (SensorManager) getSystemService(Context.SENSOR_SERVICE); } @Override protected void onResume() { Sensor sensor = manager.getDefaultSensor(Sensor.TYPE_ORIENTATION); manager....
telManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE); // 注册监听器 telManager.listen(new TelListener(), PhoneStateListener.LISTEN_CALL_STATE); } private class TelListener...