前言
Android动态加载(下)——加载已安装APK中的类和资源。
声明
博客园:http://www.cnblogs.com
农民伯伯: http://over140.cnblogs.com
Android中文Wiki:http://wikidroid.sinaapp.com
正文
一、目标
注意被调用的APK在Android系统中是已经安装的。
从当前APK中调用另外一个已安装APK的字符串、颜色值、图片、布局文件资源以及Activity。
public
class TestAActivity
extends Activity {
/**
TestB包名
*/
private
static
final String PACKAGE_TEST_B = “com.nmbb.b”;
@Override
public
void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
try {
final Context ctxTestB = getTestBContext();
Resources res = ctxTestB.getResources();
//
获取字符串string
String hello = res.getString(getId(res, “string”, “hello”));
((TextView) findViewById(R.id.testb_string)).setText(hello);
//
获取图片Drawable
Drawable drawable = res
.getDrawable(getId(res, “drawable”, “testb”));
((ImageView) findViewById(R.id.testb_drawable))
.setImageDrawable(drawable);
//
获取颜色值
int color = res.getColor(getId(res, “color”, “white”));
((TextView) findViewById(R.id.testb_color))
.setBackgroundColor(color);
//
获取布局文件
View view = getView(ctxTestB, getId(res, “layout”, “main”));
LinearLayout layout = (LinearLayout) findViewById(R.id.testb_layout);
layout.addView(view);
//
启动TestB Activity
findViewById(R.id.testb_activity).setOnClickListener(
new OnClickListener() {
@Override
public
void onClick(View v) {
try {
@SuppressWarnings(“rawtypes”)
Class cls = ctxTestB.getClassLoader()
.loadClass(“com.nmbb.TestBActivity”);
startActivity(
new Intent(ctxTestB, cls));
}
catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
});
}
catch (NameNotFoundException e) {
e.printStackTrace();
}
}
/**
* 获取资源对应的编
*
*
@param
testb
*
@param
resName
*
@param
resType
* layout、drawable、string
*
@return
*/
private
int getId(Resources testb, String resType, String resName) {
return testb.getIdentifier(resName, resType, PACKAGE_TEST_B);
}
/**
* 获取视图
*
*
@param
ctx
*
@param
id
*
@return
*/
public View getView(Context ctx,
int id) {
return ((LayoutInflater) ctx
.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(id,
null);
}
/**
* 获取TestB的Context
*
*
@return
*
@throws
NameNotFoundException
*/
private Context getTestBContext()
throws NameNotFoundException {
return createPackageContext(PACKAGE_TEST_B,
Context.CONTEXT_IGNORE_SECURITY | Context.CONTEXT_INCLUDE_CODE);
private
static HashMap getR(Class cls)
throws ClassNotFoundException, InstantiationException, IllegalAccessException {
HashMap result =
new HashMap();
for (Class r : cls.getClasses()) {
if (!r.getName().endsWith(“styleable”)) {
Object owner = r.newInstance();
for (Field field : r.getFields()) {
result.put(field.getName(), field.getInt(owner));
}
}
}
return result;

}
四、下载
五、文章
结束
如果是做大面积的换肤,还比较复杂,这种方式也不是很方便,这也是为什么现在市面上做换肤的很少,有也是很简单的换肤。这几天想到的另外一个方案,还没有实践,有效果了再拿出来分享,欢迎大家交流 :)
相关资源:丝柏人像美肤处理软件CPAC Imaging Pro 3绿化汉化破解版
声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!