使用assets目录来实现插件机制

/**
* 管理接口。
* @author jevan
* @version 1.0 at 2013-12-6
*
*/
public interface IManage {
/**
* 注册平台接口。
* @param param 传入参数,可选。
*/
public boolean regPlatform(String param);
}

插件管理类:

/**
* @author jevan
* @version 1.0 at 2013-12-6 用于初始化平台信息
*/
private static void initPlatformInstance(Context context) { String path = context.getFilesDir().getAbsolutePath() + "/jar/";
String[] files = null; File fpath = new File(path);
if (!fpath.exists()) {
fpath.mkdirs();
}
try {// 遍历assest文件夹,读取压缩包及安装包
files = context.getAssets().list("");
} catch (IOException e) {
e.printStackTrace();
} if (files == null) {
return;
} List<String> apkList = new ArrayList<String>();
// 动态绑定,运行实现了这个接口的类的方法
for (String fileName : files) {
if (fileName.endsWith(".apk")) {
Log.i("fileName", "src files: " + fileName);
Log.i("fileName", "dst files: " + path + fileName);
copy(context, fileName, path, fileName);
apkList.add(path + fileName);
}
} getPlatformInstanceVerB(context, apkList, path); } /**
* 统一平台的插件实现。
*
* @param context
* Context
* @param apkList
* 传入的apk文件列表。
*/
public static void getPlatformInstanceVerB(Context context,
List<String> apkList, String path) {
for (String file : apkList) {
Log.i("fileName", " fileName: " + file);
File jarFile = new File(file);
if (jarFile.exists()) {
DexClassLoader cl = new DexClassLoader(jarFile.toString(),
path, null, ClassLoader.getSystemClassLoader());
Class clazz = null;
Object obj = null;
try {
clazz = cl.loadClass("com.ott.porting.PortingManage");
// 对这个类进行实例化
obj = clazz.newInstance(); } catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
// 如果有这个插件,就进行下面的动作,如果没有这个插件就返回
if (clazz == null) {
return;
} try {
IManage manage = (IManage) obj;
manage.regPlatform(null);
} catch (Exception e) {
e.printStackTrace();
}
// 把Object转换成接口类型
if(obj instanceof IManage)
{
Log.i("fileName", " obj is IManage! ");
IManage manage = (IManage) obj;
manage.regPlatform(null);
}
else
{
Log.i("fileName", " obj is not IManage! ");
}
}
}
}

copy函数的实现:

     /**
* 拷贝assets目录下的文件到 savePath
*
* @param myContext
* @param ASSETS_NAME
* 要复制的文件名
* @param savePath
* 要保存的路径
* @param saveName
* 复制后的文件名 testCopy(Context context)是一个测试例子。
*/
public static void copy(Context myContext, String ASSETS_NAME,
String savePath, String saveName) {
String filename = savePath + "/" + saveName; File dir = new File(savePath);
// 如果目录不中存在,创建这个目录
if (!dir.exists())
dir.mkdir();
try {
if (!(new File(filename)).exists()) {
InputStream is = myContext.getResources().getAssets()
.open(ASSETS_NAME);
FileOutputStream fos = new FileOutputStream(filename);
byte[] buffer = new byte[2048];
int count = 0;
while ((count = is.read(buffer)) > 0) {
fos.write(buffer, 0, count);
}
fos.close();
is.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}

有个异常:

 Android java.lang.ClassCastException: cannot be cast to

抽时间找下解决方式。

2013-12-11更新:

1、解决Android java.lang.ClassCastException: cannot be cast to这个异常:

     /**
* apk的插件另外一种实现。
*
* @param context
* Context
* @param apkList
* 传入的apk文件列表。
*/
public static void getPlatformInstanceVerB(Context context,
List<String> apkList, String path) {
for (String file : apkList) {
Log.i("fileName", " fileName: " + file);
File jarFile = new File(file);
if (jarFile.exists()) {
DexClassLoader cl = new DexClassLoader(jarFile.toString(),
path, null, ClassLoader.getSystemClassLoader());
Class clazz = null;
Object instance = null;
try {
clazz = cl.loadClass("com.ott.porting.PortingManage");
Constructor localConstructor = clazz.getConstructor(new Class[] {}); instance = localConstructor.newInstance(new Object[] {}); //无参数方法
//Method des = clazz.getMethod("regPlatform");
//des.invoke(instance); //有参数方法
Method methodRegPlatform = clazz.getDeclaredMethod("regPlatform", new Class[] { String.class });
methodRegPlatform.setAccessible(true);
methodRegPlatform.invoke(instance, "test for jevan");
// 对这个类进行实例化
//obj = clazz.newInstance(); } catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
// 如果有这个插件,就进行下面的动作,如果没有这个插件就返回
if (clazz == null) {
return;
} // // 把Object转换成接口类型
// if (obj instanceof IManage) {
// Log.i("fileName", " obj is IManage! ");
// IManage manage = (IManage) obj;
// manage.regPlatform(null);
// } else {
// Log.i("fileName", " obj is not IManage! ");
// }
}
}
}
上一篇:Nginx 502错误触发条件与解决办法汇总(转载)


下一篇:MySQL表字段长度的限制