博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
转 android 动态加载 插件模型开发
阅读量:4697 次
发布时间:2019-06-09

本文共 5841 字,大约阅读时间需要 19 分钟。

目前市面上的应用商店,不管是 apple 还是 android 平台, 一般只有一家商店。

如果要动态添加商店,允许多家商店共存。
搭建一个平台,多家应用商店可以加入。
类似于商场与专卖店的关系。
每个商店的业务由各自实现,但统一由商场来提供接口供用户选择。
下面就来简单做个原型:
1 ClassLoadTestMain  商场
2 ClassLoadTestPlugin 商店

3 PluginInterface 商场提供给商店的接口

 

接口定义:

 

  1. package com.pathfindeng.android.test;  
  2.   
  3. public interface  IPlugin {  
  4.       
  5.     public int add(int a, int b);  
  6.   
  7. }  

package com.pathfindeng.android.test; public interface IPlugin { public int add(int a, int b); }

商店实现接口:

 

 

  1. package com.pathfindeng.android.test.plugin;  
  2.   
  3. import com.pathfindeng.android.test.IPlugin;  
  4.   
  5. public class Plugin implements IPlugin{  
  6.       
  7.     public int add(int a, int b){  
  8.         return a + b;  
  9.     }  
  10. }  

package com.pathfindeng.android.test.plugin; import com.pathfindeng.android.test.IPlugin; public class Plugin implements IPlugin{ public int add(int a, int b){ return a + b; } }

商场调用商店的实现:

 

 

  1. package com.pathfindeng.android.test.main;  
  2.   
  3. import com.pathfindeng.android.test.IPlugin;  
  4. import com.pathfindeng.android.test.main.R;  
  5.   
  6. import dalvik.system.DexClassLoader;  
  7. import dalvik.system.VMStack;  
  8. import android.app.Activity;  
  9. import android.os.Bundle;  
  10. import android.widget.TextView;  
  11. import android.widget.Toast;  
  12.   
  13. public class ClassLoadTestMainActivity extends Activity {  
  14.       
  15.       
  16.     TextView result;  
  17.     /** Called when the activity is first created. */  
  18.     @Override  
  19.     public void onCreate(Bundle savedInstanceState) {  
  20.         super.onCreate(savedInstanceState);  
  21.         setContentView(R.layout.main);  
  22.         result = (TextView)findViewById(R.id.result);  
  23.           
  24.     }  
  25.       
  26.     public void doInPlugin(){  
  27.           
  28.         String dexPath, dexOutputDir, libPath ,className;  
  29.         ClassLoader parent;  
  30.           
  31.         dexPath = "/data/app/com.pathfindeng.android.test.plugin-1.apk";  
  32.         dexOutputDir = "/data/data/com.pathfindeng.android.test.main";  
  33.           
  34.         libPath = "/data/data/com.pathfindeng.android.test.main/lib";  
  35.           
  36.         parent = ClassLoadTestMainActivity.this.getClassLoader();  
  37.           
  38.         //parent = VMStack.getCallingClassLoader();   
  39.           
  40.         DexClassLoader mDexClassLoader = new DexClassLoader(dexPath, dexOutputDir, libPath, parent);  
  41.           
  42.         className = "com.pathfindeng.android.test.plugin.Plugin";  
  43.           
  44.         try {  
  45.             Class mClass = mDexClassLoader.loadClass(className);  
  46.               
  47.             IPlugin mIPlugin = (IPlugin)mClass.newInstance();  
  48.               
  49.             int c;  
  50.             c = mIPlugin.add(100200);  
  51.               
  52.             result.setText("from plugin : "+c);  
  53.               
  54.             Toast.makeText(ClassLoadTestMainActivity.this"from plugin : "+c, Toast.LENGTH_LONG);  
  55.                           
  56.               
  57.         } catch (ClassNotFoundException e) {  
  58.             // TODO Auto-generated catch block   
  59.             e.printStackTrace();  
  60.         } catch (IllegalAccessException e) {  
  61.             // TODO Auto-generated catch block   
  62.             e.printStackTrace();  
  63.         } catch (InstantiationException e) {  
  64.             // TODO Auto-generated catch block   
  65.             e.printStackTrace();  
  66.         }  
  67.           
  68.           
  69.     }  
  70.   
  71.     /* (non-Javadoc) 
  72.      * @see android.app.Activity#onResume() 
  73.      */  
  74.     @Override  
  75.     protected void onResume() {  
  76.         // TODO Auto-generated method stub   
  77.         super.onResume();  
  78.           
  79.         doInPlugin();  
  80.     }  
  81.   
  82.     /* (non-Javadoc) 
  83.      * @see android.app.Activity#onDestroy() 
  84.      */  
  85.     @Override  
  86.     protected void onDestroy() {  
  87.         // TODO Auto-generated method stub   
  88.         super.onDestroy();  
  89.     }  
  90.       
  91. }  

package com.pathfindeng.android.test.main; import com.pathfindeng.android.test.IPlugin; import com.pathfindeng.android.test.main.R; import dalvik.system.DexClassLoader; import dalvik.system.VMStack; import android.app.Activity; import android.os.Bundle; import android.widget.TextView; import android.widget.Toast; public class ClassLoadTestMainActivity extends Activity { TextView result; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); result = (TextView)findViewById(R.id.result); } public void doInPlugin(){ String dexPath, dexOutputDir, libPath ,className; ClassLoader parent; dexPath = "/data/app/com.pathfindeng.android.test.plugin-1.apk"; dexOutputDir = "/data/data/com.pathfindeng.android.test.main"; libPath = "/data/data/com.pathfindeng.android.test.main/lib"; parent = ClassLoadTestMainActivity.this.getClassLoader(); //parent = VMStack.getCallingClassLoader(); DexClassLoader mDexClassLoader = new DexClassLoader(dexPath, dexOutputDir, libPath, parent); className = "com.pathfindeng.android.test.plugin.Plugin"; try { Class mClass = mDexClassLoader.loadClass(className); IPlugin mIPlugin = (IPlugin)mClass.newInstance(); int c; c = mIPlugin.add(100, 200); result.setText("from plugin : "+c); Toast.makeText(ClassLoadTestMainActivity.this, "from plugin : "+c, Toast.LENGTH_LONG); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InstantiationException e) { // TODO Auto-generated catch block e.printStackTrace(); } } /* (non-Javadoc) * @see android.app.Activity#onResume() */ @Override protected void onResume() { // TODO Auto-generated method stub super.onResume(); doInPlugin(); } /* (non-Javadoc) * @see android.app.Activity#onDestroy() */ @Override protected void onDestroy() { // TODO Auto-generated method stub super.onDestroy(); } }

从上面的代码看,调用插件还是强制定义的,不够人性

接下来做些改进。

插件端 注册 固定 Action Name  

 

  1. <activity android:name=".商店Activity" android:label="@string/app_name">  
  2.             <intent-filter>  
  3.                 <action android:name="接口指定固定 Action Name" />  
  4.             </intent-filter>  
  5. </activity>  

<activity android:name=".商店Activity" android:label="@string/app_name"> <intent-filter> <action android:name="接口指定固定 Action Name" /> </intent-filter> </activity>

 

 

服务器端 商场

利用 PackageManager 查询所有注册了接口定义的 Action Name 的商店,并获得信息。

 

  1. Intent intent = new Intent(Constants.ACTION_PLUGIN, null);  
  2. PackageManager pm = mContext.getPackageManager();  
  3. List<ResolveInfo> plugins = pm.queryIntentActivities(intent, 0);  

Intent intent = new Intent(Constants.ACTION_PLUGIN, null); PackageManager pm = mContext.getPackageManager(); List<ResolveInfo> plugins = pm.queryIntentActivities(intent, 0);

这样之前提到的 

 

  1. DexClassLoader(dexPath, dexOutputDir, libPath, parent)  

DexClassLoader(dexPath, dexOutputDir, libPath, parent)

 

 

 

参数就不需要 手动指定了。

 

 

待叙……

转载于:https://www.cnblogs.com/lovelili/archive/2011/12/29/2306423.html

你可能感兴趣的文章
Git Stash用法
查看>>
sql server 2008学习8 sql server存储和索引结构
查看>>
Jquery radio选中
查看>>
postgressql数据库中limit offset使用
查看>>
测试思想-集成测试 关于接口测试 Part 2
查看>>
php生成器使用总结
查看>>
T-SQL中的indexof函数
查看>>
javascript基础之数组(Array)对象
查看>>
mysql DML DDL DCL
查看>>
RAMPS1.4 3d打印控制板接线与测试1
查看>>
python with语句中的变量有作用域吗?
查看>>
24@Servlet_day03
查看>>
初级ant的学习
查看>>
memcached 细究(三)
查看>>
RSA System.Security.Cryptography.CryptographicException
查看>>
webservice整合spring cxf
查看>>
[解题报告] 100 - The 3n + 1 problem
查看>>
Entity Framework 学习高级篇1—改善EF代码的方法(上)
查看>>
Mybatis逆向工程配置文件详细介绍(转)
查看>>
String类的深入学习与理解
查看>>