Android uses the Apkplug framework to implement a UI replacement for the main application and plug-in communication (passing arbitrary objects)

Source: Internet
Author: User

Time in a hurry, at first glance has been half a year passed, after the six months of hard toil today eventually full of blood resurrection.

Use the Apkplug framework to dynamically replace the UI elements in the host activity. To be able to replace the UI style without updating the app.

Look first:

First, understand the basic concepts of OSGI services, such as

1. First define a Java interface (interface) to standardize the communication protocol between the host and the plug-in

Interface Com.apkplug.osgi.service.showView

void ShowView (Bundle bundle,view v,int index); Join view

void Removeview (Bundle Bundle,view v); Delete View

2. Decide the OSGi service provider and user, where we define the host application as the "OSGi service provider"and the plug-in as "OSGi service consumer".

Note: OSGi Service provider registration Service OSGI service user Query Service

3. The host application implements the ShowView interface, the corresponding class for the COM.APKPLUG.OSGI.SERVICEIMP.SHOWVIEWIMP detail code such as the following:

public class Showviewimp implements Showview{private linearlayout layout =null;/** * @param root plugin View Save UI container  */publi C Showviewimp (View root) {this.layout= (linearlayout) root;} @Overridepublic void ShowView (bundle bundle, view v, int index) {//when the plug-in finds the service and uses the callback System.out.println ("plugin comes with a view"); Layout.addview (v);} @Overridepublic void Removeview (bundle bundle, view v) {System.out.println ("plugin needs to delete a view"); Layout.removeview (v);}}


4. The host application will showviewimp as a service in the form of OSGi, detailed code in Com.apkplug.osgi.activity.ViewActivator

Attention:

1: We also virtual the host app as an OSGi Bundle (plug-in), so it has the conditions for OSGi communication with other plug-ins.

2: And the Host program (plugin) is started when the framework is started (so that the ShowView service is the first to be registered, the other plug-ins will be able to find)

3: Each OSGi bundle has an implementation bundleactivator

Viewactivator Implements Bundleactivator

Public abstract Interface bundleactivator{    //plug-in calls public    abstract void start (Bundlecontext context) throws Exception;    Call public abstract void stop when plug-in stops    (Bundlecontext context) throws Exception;}

Because the Viewactivator code is more, just post the code related to the service brochure.

... public void start (final bundlecontext context) {//Register a service to plug-in call M_reg = Context.registerservice ( ShowView.class.getName (), ShowView,            null);                  .......                  .......

5. Write the plug-in, query the ShowView service in the plugin's Bundleactivator implementation class, and initialize a view of itself to pass to the host application

The Bundleactivator implementation class for the plug-in is: Com.apkplug.osgiclient1.SimpleBundle implements Bundleactivator

The relevant code for querying the ShowView service is:

The plugin itself defines the view  mybtn        mybtn=new mybtn (Context.getbundlecontext ());       Query the ShowView service provided by the main program        servicereference ref  =  context.getservicereference (showView.class.getName ());        If  (ref!  =   null) {         //  Get service instance         ShowView Service  =  (ShowView) Context.getservice (ref);         If  (service!  =   null) {         //  Call service method          Service.showview (Context.getbundle (), MYBTN, 0);  release service, after which the service instance Context.ungetservice (ref) should not be used again          ;         }        

Summary: After the above steps, the plug-and-host view object passes and displays the process.

In conjunction with OSGi services we define Java interface classes by ourselves. and properly convert the roles of OSGi server and user to complete the various interactions we need.

Note: The OSGi service, in addition to the Query method, also provides a way for listeners (service providers can start the registration service after the user ) specific demo can also refer to Osgiservice's Printlog service registration and monitoring method.

last source code address Http://git.oschina.net/plug/OSGIService

QQ Exchange Group:132433459

Android uses the Apkplug framework to implement a UI replacement for the main application and plug-in communication (passing arbitrary objects)

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.