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

Source: Internet
Author: User

Time in a hurry, at first glance has half a year passed, after the six months of hard-worked today finally full of blood resurrection.

Use the Apkplug framework to dynamically replace UI elements in the host activity 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); Add 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 consumer query service

3. The host application implements the ShowView interface, the corresponding class is COM.APKPLUG.OSGI.SERVICEIMP.SHOWVIEWIMP specific code as follows:

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 app will register showviewimp as a service in OSGi, with specific 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 (plug-in) is launched when the framework is started (so that the ShowView service is first registered, the other plug-ins can be found later)

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, only the code related to service registration is posted here

... 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 plug-in's Bundleactivator implementation class, and initialize a view of itself as a parameter passed 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:

Plug-in Custom 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: The process of passing and displaying the plug-in and host view object is completed by the above steps.

In conjunction with OSGi services, we can accomplish the various interactions we need by customizing the Java interface classes and converting the roles of OSGi providers and users appropriately.

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

last Source Address Http://git.oschina.net/plug/OSGIService

QQ Exchange Group:132433459

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

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.