Android Plugin-Basic Principles of OSGI service in apkplug-08

Source: Internet
Author: User

 

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
5 null);

2. Service Query

Android Service

1 Intent intent=new Intent(Context,Service.class);
2 Context.bindService(intent, new ServiceConnection())
3 ...

OSGI Service

 

01 // Use the plug-in context BundleContext to query the service
02 ServiceReference ref = context.getServiceReference(Service.class.getName());
03 if (ref != null ) {
04 // Find the service
05 Service service = (Service) context.getService(ref);
06 if (service != null ) {
07 // Call the service interface
08 service.sayHello(imp);
09 }
10 // Deregister the service
11 context.ungetService(ref);
12 }

 

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.