码迷,mamicode.com
首页 > 其他好文 > 详细

插件化—通过反射调用插件的函数

时间:2016-04-30 23:31:59      阅读:237      评论:0      收藏:0      [点我收藏+]

标签:

  1 package com.zyhitheima.plugindemo;
  2 import java.lang.reflect.Method;
  3 import java.util.ArrayList;
  4 import java.util.HashMap;
  5 import java.util.List;
  6 import java.util.Map;
  7 
  8 import dalvik.system.PathClassLoader;
  9 
 10 import android.os.Bundle;
 11 import android.view.View;
 12 import android.view.View.OnClickListener;
 13 import android.widget.Button;
 14 import android.widget.LinearLayout;
 15 import android.widget.Toast;
 16 import android.app.Activity;
 17 import android.content.Context;
 18 import android.content.Intent;
 19 import android.content.pm.PackageInfo;
 20 import android.content.pm.PackageManager;
 21 import android.content.pm.PackageManager.NameNotFoundException;
 22 import android.graphics.Color;
 23 
 24 public class MainActivity extends Activity {
 25     private LinearLayout ll;
 26     @Override
 27     protected void onCreate(Bundle savedInstanceState) {
 28         super.onCreate(savedInstanceState);
 29         setContentView(R.layout.activity_main);
 30         
 31         ll = (LinearLayout) findViewById(R.id.ll);
 32         
 33         List<Map<String, Object>> plugins = new ArrayList<Map<String, Object>>();
 34         plugins = findPlugins();
 35         if(plugins.size() > 0){
 36             attachPlugin(plugins);
 37         }else{
 38             Toast.makeText(this, "没有相应的插件", 1).show();
 39         }
 40         
 41     }
 42     
 43     private void attachPlugin(List<Map<String, Object>> plugins) {
 44         if(plugins.size() > 0){
 45             for(Map<String, Object> map : plugins){
 46                 Button button = new Button(this);
 47                 button.setTextColor(Color.RED);
 48                 String label = (String) map.get("label");
 49                 final String pkgName = (String) map.get("pkgName");
 50                 button.setText(label);
 51                 
 52                 ll.addView(button);
 53                 button.setOnClickListener(new OnClickListener() {
 54                     @Override
 55                     public void onClick(View v) {
 56                         callPluginMethod(pkgName);
 57                     }
 58                 });
 59             }
 60         }
 61     }
 62     
 63     private List<Map<String, Object>> findPlugins() {
 64         List<Map<String, Object>> plugins = new ArrayList<Map<String, Object>>();
 65         //包管理器
 66         PackageManager pm = this.getPackageManager();
 67         List<PackageInfo> installPackages = pm.getInstalledPackages(PackageManager.GET_UNINSTALLED_PACKAGES);//获取所有安装的package包
 68         for(PackageInfo info : installPackages){
 69             String pkgName = info.packageName;
 70             String sharedUserId = info.sharedUserId;
 71             if(sharedUserId == null || !sharedUserId.equals("com.zyh.plugindemo") || pkgName.endsWith(this.getPackageName())){
 72                 continue;
 73             }
 74             
 75             String label = (String) pm.getApplicationLabel(info.applicationInfo);
 76             
 77             Map<String, Object> map = new HashMap<String, Object>();
 78             map.put("label", label);
 79             map.put("pkgName", pkgName);
 80             plugins.add(map);
 81         }
 82         return plugins;
 83     }
 84     
 85     private void callPluginMethod(String packageName){
 86         Context pluginContext;
 87         try {
 88             pluginContext = this.createPackageContext(packageName, CONTEXT_IGNORE_SECURITY|CONTEXT_INCLUDE_CODE);
 89             //System.out.println(pluginContext.getPackageResourcePath());
 90             PathClassLoader pathClassLoader = new PathClassLoader(pluginContext.getPackageResourcePath(), ClassLoader.getSystemClassLoader());
 91             Class<?> clazz = Class.forName(packageName + ".Demo",true,pathClassLoader);
 92             Method info = clazz.getDeclaredMethod("info", new Class[]{});
 93             info.invoke(null, null);
 94             
 95         } catch (Exception e) {
 96             // TODO Auto-generated catch block
 97             e.printStackTrace();
 98         }
 99     }
100 
101 }

 

插件化—通过反射调用插件的函数

标签:

原文地址:http://www.cnblogs.com/zhongyinghe/p/5449664.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!