Android 插件开发入门随笔(1)

本文详细介绍了一种基于两个特定JAR包实现的Android插件化开发方案,包括主程序和插件模块的开发步骤。主程序通过指定的共享用户ID进行配置,并利用特定API来查找、组装和调用插件功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

用到的jar包 ( 1.android_plugin_framework_v0.3.jar,2.android_plugin_demo_description.jar)

jar包2是里面只有一个描述类,可自定义



主程序开发:导入两个jar包

1.在清单文件 加上(sharedUserID 为宿主的标记 标记要一致)

android:sharedUserId="xxx.xxx.xxx"
2.查找插件:

PluginSearch psearch = new PluginSearch();
List<Plugin> plugins = psearch.getPlugins(this);

3.将插件再组装一下:

PluginBuilder pbuilder = new PluginBuilder(this);
plugins = pbuilder.buildPluginsDescrition(plugins);

4.遍历插件获取描述:

//MainDescript继承于jar的描述类            
MainDescript des = pdes.getDescription(this, plug);
des.getSubTitle();//描述类的方法
des.getDescription();
des.getDescription();
5.调用插件的方法:


List<PluginFeature> features = plug.getFeatures();
for (final PluginFeature pf : features) {
    for (final PluginFeatureMethod fm : pf.getMethods()) {
        // 添加插件,并且设置执行事件
        item.addPluginMethod(fm, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PluginInvoke pi = new PluginInvoke(MainActivity.this);
                try {
                    pi.invoke(plug, pf, fm);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }
}

6.调用插件的 Intent

// 遍历intent
Map<String, PluginIntent> intents = plug.getIntents();
Iterator<Map.Entry<String, PluginIntent>> iterator = intents.entrySet().iterator();
while (iterator.hasNext()) {
    final Map.Entry<String, PluginIntent> entry = iterator.next();
btn.setText(entry.getValue());
btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            PluginInvoke pi = new PluginInvoke(MainActivity.this);
            pi.invoke(entry.getValue());
        }});
}

插件开发:导入jar包2

1.在清单文件 加上(sharedUserID 为宿主的标记 标记要一致)

android:sharedUserId="xxx.xxx.xxx"

2.在assets目录下创建 plugin.xml文件  

<?xml version="1.0" encoding="UTF-8"?>
<!-- 插件提供的功能(类) -->
<plugin-features>
   <!-- 描述类,这个是自定义的 -->
   <description name="com.s5.test.Function"/>
   <intent action="com.s5.test.MainActivity" key="main">启动</intent>
   <!-- 这是一个功能(类) -->
   <feature name="com.s5.test.MainActivity">
      <!-- 方法列表 -->
      <!-- name 方法名 MainActivity里提供的方法callPhone()-->
      <method need-context="true" name="callPhone" >给10086打电话</method>
      <method need-context="true" name="callWeb" >打开作者博客</method>
   </feature>
</plugin-features>
3.feature 里写方法提供使用


附个demo

链接: http://pan.baidu.com/s/1mimx4xU 密码: ynny








评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值