Similarities and differences between OSGI and android Service
The OSGI Service and android Service are similar in terms of Service and Client.
Android Service interface -- service. AIDL
OSGI interface -- java interface
Therefore, the android inter-process communication Service can only transmit serialized data, while the OSGI Service can transmit any java object.
Comparison between OSGI and android Service Registration/query methods
1. Service Registration
Android Service
1 |
Intent intent=new Intent(Context,Service.class); |
2 |
Context.startService(intent); |
OSGI Service
1 |
BundleContext context; // Plugin Context |
2 |
ServiceRegistration m_reg = context.registerService( |
3 |
sayHelloImp.class.getName(),// The service name is generally the Interface Class Name |
4 |
my, // Service implementation class |
2. Service Query
Android Service
1 |
Intent intent=new Intent(Context,Service.class); |
2 |
Context.bindService(intent, new ServiceConnection()) |
OSGI Service
01 |
// Use the plug-in context BundleContext to query the service |
02 |
ServiceReference ref = context.getServiceReference(Service.class.getName()); |
05 |
Service service = (Service) context.getService(ref); |
06 |
if (service != null ) { |
07 |
// Call the service interface |
08 |
service.sayHello(imp); |
10 |
// Deregister the service |
11 |
context.ungetService(ref); |
Iii. OSGI service features
The OSGI service is a transient plug-in that may be disabled or uninstalled at any time. Therefore, we recommend that you first check whether the service still exists when using the service.
4. OSGI service considerations
When using the OSGI service, pay attention to the consistency of the java class of the service interface. The service provider and the consumer should use the same java interface (the class loader is the same). Otherwise, the type forced conversion exception may occur during service query. Generally, we provide java interfaces as service providers.