android编程时布局文件,图片资源等都是放在同一个文件夹下,这样照成一个问题就是我们想重用UI布局文件和图片时就还需要其分离这些资料,相信大部分android程序员都遇到过这样的问题,其痛苦程度不亚于世纪末日赶不上诺亚方舟。

今天我用apkplug框架实现将不同的资源放在不同的插件apk包中,然后通过插件间类查找的方式实现插件机布局文件共享。不说废话了!

一 新建一个插件myBundle1由它提供布局文件供myBundle插件调用

结合上一篇文章本章我再建一个插件工程myBundle1新增实现3个java类分别是

BundleContextFactory.java 这个类的唯一功能就是存储插件启动时获取的BundleContext,该类中有我们需要的android.content.Context

  1. public class BundleContextFactory implements BundleInstance{
  2. private static BundleContextFactory _instance=null;
  3. private BundleContext mcontext = null;
  4. synchronized public static BundleContextFactory getInstance(){
  5. if(_instance==null){
  6. _instance=new BundleContextFactory();
  7. }
  8. return _instance;
  9. }
  10. private BundleContextFactory(){
  11. }
  12. public BundleContext getBundleContext() {
  13. // TODO Auto-generated method stub
  14. return this.mcontext;
  15. }
  16. public void setBundleContext(BundleContext arg0) {
  17. // TODO Auto-generated method stub
  18. this.mcontext = arg0;
  19. }
  20. }

TBSurfaceView.java 一个SurfaceView 用于演示View

myLayout.java 继承RelativeLayout,通过它插件myBundle就可以获取插件myBundle1的布局文件了

  1. public class myLayout extends RelativeLayout{
  2. public myLayout(Context context, AttributeSet attrs) {
  3. super(context, attrs);
  4. //获取插件启动时得到的Context
  5. Context m=BundleContextFactory.getInstance().getBundleContext().getBundleContext();
  6. LayoutInflater mInflater=LayoutInflater.from(context);
  7. mInflater = mInflater.cloneInContext(m);
  8. mInflater.inflate(R.layout.main11, this, true);
  9. }
  10. }

布局文件 main11.xml

  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:paddingBottom="@dimen/activity_vertical_margin"
  6. android:paddingLeft="@dimen/activity_horizontal_margin"
  7. android:paddingRight="@dimen/activity_horizontal_margin"
  8. android:paddingTop="@dimen/activity_vertical_margin"
  9. tools:context=".MainActivity" >
  10. <TextView
  11. android:id="@+id/tt"
  12. android:layout_width="wrap_content"
  13. android:layout_height="wrap_content"
  14. android:text="我是插件myBundle1" />
  15. <com.example.mybundle1.TBSurfaceView
  16. android:layout_below="@+id/tt"
  17. android:layout_width="fill_parent"
  18. android:layout_height="fill_parent"
  19. />
  20. </RelativeLayout>

最后插件myBundle1文件目录为

二 修改插件myBundle布局文件activity_main.xml添加View com.example.mybundle1.myLayout

  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:paddingBottom="@dimen/activity_vertical_margin"
  6. android:paddingLeft="@dimen/activity_horizontal_margin"
  7. android:paddingRight="@dimen/activity_horizontal_margin"
  8. android:paddingTop="@dimen/activity_vertical_margin"
  9. tools:context=".MainActivity" >
  10. <TextView
  11. android:id="@+id/tt"
  12. android:layout_width="wrap_content"
  13. android:layout_height="wrap_content"
  14. android:text="hello_world 我是myBundle的Activity" />
  15. <com.example.mybundle1.myLayout
  16. android:layout_below="@+id/tt"
  17. android:layout_width="fill_parent"
  18. android:layout_height="fill_parent"
  19. android:id="@+id/myLayout1"/>
  20. <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"/>
  21. </RelativeLayout>

三 将两个编译好的插件添加到主应用的assets文件夹中并在PropertyInstance接口的public String[] AutoStart()中添加两个插件自动启动

  1. public String[] AutoStart() {
  2. File f0=null,f1=null;
  3. try {
  4. InputStream in=context.getAssets().open("myBundle.apk");
  5. f0=new File(context.getFilesDir(),"myBundle.apk");
  6. if(!f0.exists())
  7. copy(in, f0);
  8. } catch (IOException e) {
  9. // TODO Auto-generated catch block
  10. e.printStackTrace();
  11. }
  12. try {
  13. InputStream in=context.getAssets().open("myBundle1.apk");
  14. f1=new File(context.getFilesDir(),"myBundle1.apk");
  15. if(!f1.exists())
  16. copy(in, f1);
  17. } catch (IOException e) {
  18. // TODO Auto-generated catch block
  19. e.printStackTrace();
  20. }
  21. return new String[]{"file:"+f0.getAbsolutePath(),"file:"+f1.getAbsolutePath()};
  22. }

最后启动应用查看效果如图

最后给出源码

注意:1.以上需要注意的问题的是需要引出的类都应该在plugin.xml文件中添加Export-Package="com.example.mybundle1"  这样插件间才能找的到(下一章会实现另一种方式插件间交换类)

2.apkplug官网为:www.apkplug.com

android模块化app开发笔记-2插件间布局文件共享的更多相关文章

  1. Android移动APP开发笔记——Cordova(PhoneGap)通过CordovaPlugin插件调用 Activity 实例

    引言 Cordova(PhoneGap)采用的是HTML5+JavaScript混合模式来开发移动手机APP,因此当页面需要获取手机内部某些信息时(例如:联系人信息,坐标定位,短信等),程序就需要调用 ...

  2. Android移动APP开发笔记——最新版Cordova 5.3.1(PhoneGap)搭建开发环境

    引言 简单介绍一下Cordova的来历,Cordova的前身叫PhoneGap,自被Adobe收购后交由Apache管理,并将其核心功能开源改名为Cordova.它能让你使用HTML5轻松调用本地AP ...

  3. android模块化app开发-3远程动态更新插件

    前两章用apkplug框架实现了两个基本的功能,但它们都是在本地安装测试的,在实际开发过程中我们肯定是需要与服务器联网将更新的插件远程推送给用户手机客户端.今天利用apkplug提供的插件托管服务轻松 ...

  4. android模块化app开发-4为APP减负

    现在android应用中一个趋势是应用越来越大,免去游戏不谈普通APP也是一个个的体积直线增长.这里面除了业务增长外各种接口jar包的对接也占了不少比重.像广告SDK,统计SDK,支付SDK等这些我们 ...

  5. Android APP开发笔记

    环境搭建 windows系统上需要以下软件: android SDK -- app开发工具包, 开发运行环境(包括SDK管理工具,和虚拟设备管理). JDK -- java 开发工具包, 负责app代 ...

  6. Windows 8.1 store app 开发笔记

    原文:Windows 8.1 store app 开发笔记 零.简介 一切都要从博彦之星比赛说起.今年比赛的主题是使用Bing API(主要提到的有Bing Map API.Bing Translat ...

  7. 小学英语课文朗读APP开发笔记(一):创建Win7虚拟机

    1 缘起 以小米盒子为代表的OTT机顶盒.智能电视的快速普及,快速推动了Android技术在机顶盒.智能电视领域的普及.既然都是用的Android操作系统,那么从技术上来说应该是大同小异的,当然和手机 ...

  8. Android Stuido 提高开发效率的插件

    好久没有更新博客了,最近搞个listview搞得半死不活的,心累~~ 今天带来的是Android Studio插件的整理,全是我已经安装使用的,写这篇博文的目的也是因为我怕我自己给忘记怎么用(尴尬) ...

  9. 【Android】1.0 第1章 C#之Android手机App开发

    分类:C#.Android.VS2015:创建日期:2016-01-20 目前Android在全世界市场上大约有75%的占有率,国人Android手机的持有比例更甚,甚至达到90%以上.因此搞计算机的 ...

随机推荐

  1. LINUX下如何查看tomcat运行状态,判断其是否启动

    1,查看Tomcat启动日志. ${catalina_home}\logs [root@iZ25b4ffkfaZ logs]# tail -f catalina.outSep 10, 2015 11: ...

  2. hdu 4696 Answers

    思路:由于c[i]要么是1,要么是2.所以当c[i]中没有1的时候就不可能得到奇数: 再就是如果m<=0,也不可能得到. 代码如下: #include<cstdio> #includ ...

  3. Android中数据存储之SharedPreferences

    import android.content.Context; import android.content.SharedPreferences; import android.content.Sha ...

  4. 朴素贝叶斯方法(Naive Bayes Method)

        朴素贝叶斯是一种很简单的分类方法,之所以称之为朴素,是因为它有着非常强的前提条件-其所有特征都是相互独立的,是一种典型的生成学习算法.所谓生成学习算法,是指由训练数据学习联合概率分布P(X,Y ...

  5. POSIX semaphore: sem_open, sem_close, sem_post, sem_wait

    http://www.cnblogs.com/BloodAndBone/archive/2011/01/18/1938552.html 一.Posix有名信号灯 1.posix有名信号灯函数 函数se ...

  6. Orcle数据库查询练习复习:三

    一.题目 1.与“张三”同乡的男生姓名 select * from student where snativeplace=(select snativeplace from student where ...

  7. mapgis处理编辑属性结构时 死机问题

    上午用的好好的mapgis6.7,下午就出错了,一编辑属性结构就出错,到网上查到了解决方法,和大家共享一下. 转自:http://bbs.3s001.com/forum.php?mod=viewthr ...

  8. Java 进行 RSA 加解密时不得不考虑到的那些事儿

    1. 加密的系统不要具备解密的功能,否则 RSA 可能不太合适 公钥加密,私钥解密.加密的系统和解密的系统分开部署,加密的系统不应该同时具备解密的功能,这样即使黑客攻破了加密系统,他拿到的也只是一堆无 ...

  9. Intellij Idea 创建EJB项目入门(一)

    相关软件: 1.JBoss(jboss-as-7.1.1.Final):http://jbossas.jboss.org/downloads 2.Intellij IDEA 13.02 3.JDK 1 ...

  10. Git for windows 中文乱码解决方案

    1.git status时显示乱码,如下: \316\304\261\276\316\304\265\265.txt 解决方案: $ git config --global core.quotepat ...