android模块化app开发笔记-2插件间布局文件共享
android编程时布局文件,图片资源等都是放在同一个文件夹下,这样照成一个问题就是我们想重用UI布局文件和图片时就还需要其分离这些资料,相信大部分android程序员都遇到过这样的问题,其痛苦程度不亚于世纪末日赶不上诺亚方舟。
今天我用apkplug框架实现将不同的资源放在不同的插件apk包中,然后通过插件间类查找的方式实现插件机布局文件共享。不说废话了!
一 新建一个插件myBundle1由它提供布局文件供myBundle插件调用
结合上一篇文章本章我再建一个插件工程myBundle1新增实现3个java类分别是
BundleContextFactory.java 这个类的唯一功能就是存储插件启动时获取的BundleContext,该类中有我们需要的android.content.Context
- public class BundleContextFactory implements BundleInstance{
- private static BundleContextFactory _instance=null;
- private BundleContext mcontext = null;
- synchronized public static BundleContextFactory getInstance(){
- if(_instance==null){
- _instance=new BundleContextFactory();
- }
- return _instance;
- }
- private BundleContextFactory(){
- }
- public BundleContext getBundleContext() {
- // TODO Auto-generated method stub
- return this.mcontext;
- }
- public void setBundleContext(BundleContext arg0) {
- // TODO Auto-generated method stub
- this.mcontext = arg0;
- }
- }
TBSurfaceView.java 一个SurfaceView 用于演示View
myLayout.java 继承RelativeLayout,通过它插件myBundle就可以获取插件myBundle1的布局文件了
- public class myLayout extends RelativeLayout{
- public myLayout(Context context, AttributeSet attrs) {
- super(context, attrs);
- //获取插件启动时得到的Context
- Context m=BundleContextFactory.getInstance().getBundleContext().getBundleContext();
- LayoutInflater mInflater=LayoutInflater.from(context);
- mInflater = mInflater.cloneInContext(m);
- mInflater.inflate(R.layout.main11, this, true);
- }
- }
布局文件 main11.xml
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:paddingBottom="@dimen/activity_vertical_margin"
- android:paddingLeft="@dimen/activity_horizontal_margin"
- android:paddingRight="@dimen/activity_horizontal_margin"
- android:paddingTop="@dimen/activity_vertical_margin"
- tools:context=".MainActivity" >
- <TextView
- android:id="@+id/tt"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="我是插件myBundle1" />
- <com.example.mybundle1.TBSurfaceView
- android:layout_below="@+id/tt"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- />
- </RelativeLayout>
最后插件myBundle1文件目录为

二 修改插件myBundle布局文件activity_main.xml添加View com.example.mybundle1.myLayout
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:paddingBottom="@dimen/activity_vertical_margin"
- android:paddingLeft="@dimen/activity_horizontal_margin"
- android:paddingRight="@dimen/activity_horizontal_margin"
- android:paddingTop="@dimen/activity_vertical_margin"
- tools:context=".MainActivity" >
- <TextView
- android:id="@+id/tt"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="hello_world 我是myBundle的Activity" />
- <com.example.mybundle1.myLayout
- android:layout_below="@+id/tt"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:id="@+id/myLayout1"/>
- <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="TextView" android:layout_alignBottom="@+id/myLayout1" android:layout_centerHorizontal="true"/>
- </RelativeLayout>
三 将两个编译好的插件添加到主应用的assets文件夹中并在PropertyInstance接口的public String[] AutoStart()中添加两个插件自动启动
- public String[] AutoStart() {
- File f0=null,f1=null;
- try {
- InputStream in=context.getAssets().open("myBundle.apk");
- f0=new File(context.getFilesDir(),"myBundle.apk");
- if(!f0.exists())
- copy(in, f0);
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- try {
- InputStream in=context.getAssets().open("myBundle1.apk");
- f1=new File(context.getFilesDir(),"myBundle1.apk");
- if(!f1.exists())
- copy(in, f1);
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- return new String[]{"file:"+f0.getAbsolutePath(),"file:"+f1.getAbsolutePath()};
- }
最后启动应用查看效果如图

最后给出源码
注意:1.以上需要注意的问题的是需要引出的类都应该在plugin.xml文件中添加Export-Package="com.example.mybundle1" 这样插件间才能找的到(下一章会实现另一种方式插件间交换类)
2.apkplug官网为:www.apkplug.com
android模块化app开发笔记-2插件间布局文件共享的更多相关文章
- Android移动APP开发笔记——Cordova(PhoneGap)通过CordovaPlugin插件调用 Activity 实例
引言 Cordova(PhoneGap)采用的是HTML5+JavaScript混合模式来开发移动手机APP,因此当页面需要获取手机内部某些信息时(例如:联系人信息,坐标定位,短信等),程序就需要调用 ...
- Android移动APP开发笔记——最新版Cordova 5.3.1(PhoneGap)搭建开发环境
引言 简单介绍一下Cordova的来历,Cordova的前身叫PhoneGap,自被Adobe收购后交由Apache管理,并将其核心功能开源改名为Cordova.它能让你使用HTML5轻松调用本地AP ...
- android模块化app开发-3远程动态更新插件
前两章用apkplug框架实现了两个基本的功能,但它们都是在本地安装测试的,在实际开发过程中我们肯定是需要与服务器联网将更新的插件远程推送给用户手机客户端.今天利用apkplug提供的插件托管服务轻松 ...
- android模块化app开发-4为APP减负
现在android应用中一个趋势是应用越来越大,免去游戏不谈普通APP也是一个个的体积直线增长.这里面除了业务增长外各种接口jar包的对接也占了不少比重.像广告SDK,统计SDK,支付SDK等这些我们 ...
- Android APP开发笔记
环境搭建 windows系统上需要以下软件: android SDK -- app开发工具包, 开发运行环境(包括SDK管理工具,和虚拟设备管理). JDK -- java 开发工具包, 负责app代 ...
- Windows 8.1 store app 开发笔记
原文:Windows 8.1 store app 开发笔记 零.简介 一切都要从博彦之星比赛说起.今年比赛的主题是使用Bing API(主要提到的有Bing Map API.Bing Translat ...
- 小学英语课文朗读APP开发笔记(一):创建Win7虚拟机
1 缘起 以小米盒子为代表的OTT机顶盒.智能电视的快速普及,快速推动了Android技术在机顶盒.智能电视领域的普及.既然都是用的Android操作系统,那么从技术上来说应该是大同小异的,当然和手机 ...
- Android Stuido 提高开发效率的插件
好久没有更新博客了,最近搞个listview搞得半死不活的,心累~~ 今天带来的是Android Studio插件的整理,全是我已经安装使用的,写这篇博文的目的也是因为我怕我自己给忘记怎么用(尴尬) ...
- 【Android】1.0 第1章 C#之Android手机App开发
分类:C#.Android.VS2015:创建日期:2016-01-20 目前Android在全世界市场上大约有75%的占有率,国人Android手机的持有比例更甚,甚至达到90%以上.因此搞计算机的 ...
随机推荐
- C# MessageBox 用法大全(转)
C# MessageBox 用法大全 http://www.cnblogs.com/Tammie/archive/2011/08/05/2128623.html 我们在程序中经常会用到MessageB ...
- POJ 1699 Best Sequence(DFS)
題目鏈接 題意 : 將幾個片段如圖所示方法縮成一個序列,求出最短這個序列. 思路 : 其實我也不知道怎麼做.....看網上都用了DP.....但是我不會.....這個DP不錯,還有用KMP+状压DP做 ...
- VS2010彻底卸载
下载Microsoft Visual Studio 2010 Uninstall Utility来移除,默认情况下,这将删除 Visual Studio 和支持组件,但不会删除与计算机上的其他应用程序 ...
- lintcode:Length of Last Word 最后一个单词的长度
题目: 最后一个单词的长度 给定一个字符串, 包含大小写字母.空格' ',请返回其最后一个单词的长度. 如果不存在最后一个单词,请返回 0 . 样例 给定 s = "Hello World& ...
- 漫谈C语言及如何学习C语言
抄自:http://my.oschina.net/apeng/blog/137911 目录:[ - ] 为什么要学习C语言? C语言学习方法 1,参考书籍 2,动手实验环境搭建 3,网络资源 附录 一 ...
- Java学习笔记之:Java的变量
一.介绍 在Java语言中,所有的变量在使用前必须声明.声明变量的基本格式如下: type identifier [ = value][, identifier [= value] ...] ; 格式 ...
- VCL源码分析方法论(以TButton.Caption属性的由来为例)
最近一段时间似乎流行源码分析:)我也来谈谈在过去一段时间里对VCL源码的分析方法方面的一点体会,本文将不探讨VCL类库的构架和设计模式方面的东本,只是以我们常见的控件属性/方法的实现过程作简单的说明, ...
- 深入探索 Java 热部署
在 Java 开发领域,热部署一直是一个难以解决的问题,目前的 Java 虚拟机只能实现方法体的修改热部署,对于整个类的结构修改,仍然需要重启虚拟机,对类重新加载才能完成更新操作.对于某些大型的应用来 ...
- Mac + IDEA + JRebel破解方法.
[重要提示]---最佳人生 一.只推荐当计算机无法访问互联网时使用本破解文件. 二.如果可以访问互联网,建议直接到JRebel官网注册JRebel会员获取[正版永久免费]使用的授权码.JRebel会员 ...
- Image.FrameDimensionsList 属性-----具体使用案例
上一篇中说到了图片的具体产生以及属性,本篇主要是具体的使用,详情案例见下面的具体视图及代码 using System;using System.Collections.Generic;using Sy ...