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

0 0
原创粉丝点击