Android uses the apkplug framework to implement communication between the main application and plug-in (passing any object) and implement UI replacement.

Source: Internet
Author: User

Time is in a hurry. At first glance, half a year has passed. after half a year of hard work, we have finally been revived with blood.

The apkplug framework is used to dynamically Replace the UI elements in the host Activity, so that the UI style can be changed without updating the application.

First look:

First, understand the basic concepts of the OSGI service, such

1. Define a java interface to regulate the communication protocol between the host and the plug-in.

Interface com.apk plug. osgi. service. showView

Void showView (Bundle bundle, View v, int index); // Add View

Void removeView (Bundle bundle, View v); // delete a View

2. Determine the osgi service provider and user. Here we define the Host application as "OSGI service provider" and the plug-in as "OSGI service user ".

Note: OSGI service provider registration service OSGI service user query service

3. The host application implements the showView interface. The corresponding class is com.apk plug. osgi. serviceImp. showViewImp. The specific code is as follows:

Public class showViewImp implements showView {private LinearLayout layout = null;/*** @ param root plug-in View Save the UI container */public showViewImp (View root) {this. layout = (LinearLayout) root;} @ Overridepublic void showView (Bundle bundle, View v, int index) {// callback System when the plug-in finds and uses the service. out. println (" ins send a View"); layout. addView (v) ;}@ Overridepublic void removeView (Bundle bundle, View v) {System. out. println (" ins need to delete a View"); layout. removeView (v );}}


4.The client application uses showviewimpto register services in the form of osgi, and the specific code is in com.apk plug. osgi. activity. ViewActivator.

Note:

1: we virtualize the Host application as an OSGI Bundle (plug-in) so that it has the condition for OSGI communication with other plug-ins.

2: The Host Program (plug-in) is started when the framework is started. (In this way, the showView service is first registered, and other plug-ins can be found later)

3: Each OSGI Bundle has a BundleActivator.

ViewActivator implements BundleActivator

Public abstract interface BundleActivator {// call public abstract void start (BundleContext context) throws Exception when the plug-in is started; // call public abstract void stop (BundleContext context) throws Exception when the plug-in is stopped ;}

Because ViewActivator has many Code, only the Code related to service registration is pasted here.

........ Public void start (final BundleContext context) {// register a service and call m_reg = context for the plug-in. registerService (showView. class. getName (), showView, null );..............

5. Compile the plug-in to query the showView service in the BundleActivator implementation class of the plug-in, and initialize a View as a parameter and pass it to the Host application.

The BundleActivator implementation class of the plug-in is com.apk plug. osgiclient1.SimpleBundle implements BundleActivator.

The code for querying the showView service is:

// Plug-in custom View myBtn = new myBtn (context. getBundleContext (); // query the showView service ServiceReference ref = context provided by the main program. getServiceReference (showView. class. getName (); if (ref! = Null) {// get Service instance showView service = (showView) context. getService (ref); if (service! = Null) {// call the Service method service. showView (context. getBundle (), myBtn, 0); // release the Service. You should not continue to use the Service instance context. ungetService (ref );}}

Conclusion: After the above steps, the process of transferring and displaying the View object of the plug-in> host is completed.

In combination with the OSGI service, we can customize the java interface class and convert the role of the OSGI service provider and the user to complete the various interactions we need.

Note: In addition to the query method, the OSGI Service also provides the listener method (the service provider can start the registration service after the user). For details about the demo, refer to the printLog Service Registration and listening method of osgiService.

Last Source Code address http://git.oschina.net/plug/OSGIService

QQ chat group: 132433459

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.