天天看点

android插件化-apkplugdemo源码阅读指南-10

阅读本节内容前可先了解 

本教程是基于apkplug V1.6.8 版本编写  最新开发方式以官网为准

可下载最新的apkplugdemo源码

apkplugdemo演示图

一 apkplugdemo工程源码结构

    src

        |-com.apkplugdemo.adapter             --插件列表Adapter

        |-com.apkplugdemo.adapter.base      --adapter 基类

        |-com.apkplugdemo.FileUtil               --文件操作类

        |-com.apkplugdemo.FileUtil.filter        --文件类型过滤类

        |-com.apkplugdemo.util                    --项目通用工具类

        |-com.apkplugdemo.util.Observer       --java观察者设计模式类

        |-com.apkplugdemo.util.preferencesFactory  --preferences操作类

        |-com.example.apkplugdemo             --项目application 和activity类

        |-huahua.viewpager                         --与com.example.apkplugdemo功能相同 只是提供fragment方式展示

二 阅读方式

    根据以上结构可以看出除去工具类我们需要阅读的代码并不多

    com.apkplugdemo.adapter.ListBundleAdapter    --负责首页列表Item展示以及 "运行"按钮事件

    com.example.apkplugdemo.ProxyApplication     --负责启动apkplug框架,以及安装assets目录下的插件 (通过InstallBundle类安装)

    com.example.apkplugdemo.MyProperty            --启动框架需要的接口类,为框架提供本地化变量保存于获取的接口 (老版本还提供自启插件的安装  v1.6.8版本用BundleControl服务替代)

    com.example.apkplugdemo.MainActivity           --项目启动类,展示已安装插件列表,提供安装SD卡中插件的按钮等功能 

    com.example.apkplugdemo.InstallBundle    --启动assets目录下的插件      

三 ProxyApplication

    ProxyApplication 只启动框架 然后调用InstallBundle启动插件

<code>01</code>

<code>public</code> <code>void</code> <code>onCreate() {  </code>

<code>02</code>

<code>    </code><code>super</code><code>.onCreate();</code>

<code>03</code>

<code>    </code><code>try</code><code>{</code>

<code>04</code>

<code>        </code><code>List activators=</code><code>new</code> <code>java.util.ArrayList&lt;BundleActivator&gt;();</code>

<code>05</code>

<code>        </code><code>//将服务加入框架,框架将在启动时启动这些服务</code>

<code>06</code>

<code>        </code><code>activators.add(</code><code>new</code> <code>appServiceManager());</code>

<code>07</code>

<code>       </code><code>frame=FrameworkFactory.getInstance().start(activators,</code><code>this</code><code>,</code><code>new</code> <code>MyProperty(</code><code>this</code><code>.getApplicationContext()));</code>

<code>08</code>

<code>        </code><code>BundleContext context =frame.getSystemBundleContext();</code>

<code>09</code>

<code>   </code><code>//安装assets文件夹下的插件 该类替代了MyProperty.AutoStart()方法 ,1.6.7以上建议使用新方式</code>

<code>10</code>

<code>        </code><code>InstallBundle ib=</code><code>new</code> <code>InstallBundle();</code>

<code>11</code>

<code>       </code><code>ib.installBundle(getApplicationContext(), context,</code>

<code>12</code>

<code>            </code><code>new</code> <code>installCallback(){</code>

<code>13</code>

<code>                </code><code>@Override</code>

<code>14</code>

<code>                </code><code>public</code> <code>void</code> <code>callback(</code><code>int</code> <code>arg0, Bundle arg1) {</code>

<code>15</code>

<code>                    </code><code>if</code><code>(arg0==installCallback.stutas5||arg0==installCallback.stutas7){</code>

<code>16</code>

<code>            </code><code>Log.d(</code><code>""</code><code>,String.format(</code><code>"插件安装 %s : %d"</code><code>,arg1.getName(),arg0));</code>

<code>17</code>

<code>                    </code><code>return</code><code>;</code>

<code>18</code>

<code>                    </code><code>}</code><code>else</code><code>{</code>

<code>19</code>

<code>                        </code><code>Log.d(</code><code>""</code><code>,</code><code>"插件安装失败 :%s"</code><code>+arg1.getName());</code>

<code>20</code>

<code>                    </code><code>}</code>

<code>21</code>

<code>                </code><code>}</code>

<code>22</code>

<code>        </code><code>});</code>

<code>23</code>

<code>       </code><code>}</code><code>catch</code> <code>(Exception ex){</code>

<code>24</code>

<code>            </code><code>System.err.println(</code><code>"Could not create : "</code> <code>+ ex);</code>

<code>25</code>

<code>            </code><code>ex.printStackTrace();</code>

<code>26</code>

<code>       </code><code>int</code> <code>nPid = android.os.Process.myPid();</code>

<code>27</code>

<code>            </code><code>android.os.Process.killProcess(nPid);</code>

<code>28</code>

<code>        </code><code>}</code>

<code>29</code>

<code>}</code>

四 InstallBundle 安装插件实现

    InstallBundle 是调用BundleControl实现将assets目录中的apk文件安装到宿主应用中的,详细可看 &lt;&gt;

五 MainActivity 界面代码

    MainActivity 初始化函数

        initBundleList()              -- 获取已安装插件  &lt;&gt;

        ListenerBundleEvent()    --监听插件安装事件 &lt;&gt;

apkplugdemo有关于apkplug框架的调用就是这些了,其他工具性的代码感兴趣的同学可以自己看。