android动态换肤系列4——从apk文件中获取Resources对象(下)
来源:互联网 发布:adobe flash play mac 编辑:程序博客网 时间:2024/06/05 09:20
这个示例演示如何从外部apk文件中获取color值并在本地使用
ColorActivity.java文件
package com.thinking.skintest;
import java.io.File;
import java.lang.reflect.Method;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import com.thinking.skintest.ReadColor.IListener;
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.res.AssetManager;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.content.res.Resources.NotFoundException;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class ColorActivity extends Activity implements IListener {
private Button btn_start;
private TextView txt_pk;
private TextView txt_color;
private Handler mHandler = new Handler();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_color);
btn_start = (Button) findViewById(R.id.btn_start);
txt_pk = (TextView) findViewById(R.id.txt_pk);
txt_color = (TextView) findViewById(R.id.txt_color);
btn_start.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
new ReadColor().doRead(ColorActivity.this, ColorActivity.this,
R.color.test);
}
});
}
@Override
public void onReadPkName(final String pk_name) {
mHandler.post(new Runnable() {
@Override
public void run() {
txt_pk.setText(pk_name);
}
});
}
@Override
public void onReadColor(final int color) {
mHandler.post(new Runnable() {
@Override
public void run() {
txt_color.setText(color + "");
btn_start.setBackgroundColor(color);
}
});
}
}
class ReadColor {
public static ExecutorService pool = Executors.newFixedThreadPool(1);
private static String SDCardPath = Environment
.getExternalStorageDirectory().getPath();
public interface IListener {
public void onReadPkName(String pk_name);
public void onReadColor(int color);
}
public void doRead(final Context context, final IListener listener,
final int color_id) {
pool.execute(new Runnable() {
@Override
public void run() {
// 制作text.thinking压缩文件,文件目录与本apk相同
File file = new File(SDCardPath + "/" + "text.thinking");
// 获取text.thinking的packageName
PackageManager mPm = context.getPackageManager();
PackageInfo mInfo = mPm.getPackageArchiveInfo(
file.getAbsolutePath(), PackageManager.GET_ACTIVITIES);
String pk_name = mInfo.packageName;
listener.onReadPkName(pk_name);// 需要READ_EXTERNAL_STORAGE权限
// 通过反射获取text.thinking的参数
AssetManager assetManager = null;
try {
assetManager = AssetManager.class.newInstance();
Method addAssetPath = assetManager.getClass().getMethod(
"addAssetPath", String.class);
addAssetPath.invoke(assetManager, file.getAbsolutePath());
} catch (Exception e) {
e.printStackTrace();
}
// 用本地Resources的参数(屏幕、配置信息等)实例化
DisplayMetrics disp = context.getResources()
.getDisplayMetrics();
Configuration config = context.getResources()
.getConfiguration();
Resources newResource = new Resources(assetManager, disp,
config);
listener.onReadColor(doReadColor(context, listener,
newResource, color_id, pk_name));
}
});
}
private int doReadColor(Context context, IListener listener,
Resources newResource, int color_id, String pk_name) {
int originColor = context.getResources().getColor(color_id);
// 如果读取Resource失败,则用原有的Resource
if (newResource == null) {
return originColor;
}
// 找到本地资源id对应的资源name
String resName = context.getResources().getResourceEntryName(color_id);
// 用资源name找到在外界Resource里的资源id
int trueResId = newResource.getIdentifier(resName, "color", pk_name);
int trueColor = 0;
try {
trueColor = newResource.getColor(trueResId);
} catch (NotFoundException e) {
e.printStackTrace();
trueColor = originColor;
}
return trueColor;
}
}
activity_color.xml文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/btn_start"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/test"
android:text="Start" />
<TextView
android:id="@+id/txt_pk"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="12sp" />
<TextView
android:id="@+id/txt_color"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="12sp" />
</LinearLayout>
1~4工程源码:http://download.csdn.net/detail/yongyu_it/9448891
- android动态换肤系列4——从apk文件中获取Resources对象(下)
- android动态换肤系列3——从apk文件中获取Resources对象(上)
- Android换肤系列 Resources
- Android动态换肤(三、安装主题apk方式)
- Android资源访问机制——获取Resources对象
- Android资源访问机制——获取Resources对象
- Android资源访问机制—获取Resources对象
- Android资源访问机制—获取Resources对象
- Android换肤apk
- android动态换肤系列1——android命名空间的使用
- android动态换肤系列2——android.view.LayoutInflater.Factory的使用
- Android Studio问题解决系列—Resources中getDrawable(int)过时
- Android 以apk包方式共享资源(动态换肤)的实现方式
- Android动态换肤(二、apk免安装插件方式)
- Android 以apk包方式共享资源(动态换肤)的实现方式
- android换肤功能中,如何动态获取控件中背景图片的资源id?
- Android应用开发提高系列(5)——Android动态加载(下)——加载已安装APK中的类和资源
- Android应用开发提高系列(5)——Android动态加载(下)——加载已安装APK中的类和资源
- Fragment栈回退和传参方式
- Linux常用命令大全
- netfilter/iptables全攻略
- 第四届_错误票据
- java事件监听机制(观察者设计模式的实际运用)
- android动态换肤系列4——从apk文件中获取Resources对象(下)
- jQuery--引入jq&jq简单方法
- select()函数
- c#中的报表简单操作(Excel)
- Overlay之VXLAN架构
- 欢迎使用CSDN-markdown编辑器
- Linux 平台下 YUM 源配置 手册
- Android Fragment嵌套使用存在的一些BUG
- org.eclipse.core.runtime.CoreException: Plug-in org.eclipse.jdt.ui was unable to load class org.ecli