Closed JiZhi-Error closed 3 years ago
我之前写了个工具类,就是通过包名,反射获取资源id,value,这个你要么,要的话我回头整理下发你,你塞里吧
哈哈,你发下,我看下具体实现什么功能,有必要的话可以放进去,目前有获取资源 id 的 ResourceUtils getIdentifier 方法
/**
* 获取资源 id
* @param resName 资源名
* @param defType 资源类型
* @param packageName 应用包名
* @return 资源 id
*/
public static int getIdentifier(final String resName, final String defType, final String packageName) {
try {
return DevUtils.getContext().getResources().getIdentifier(resName, defType, packageName);
} catch (Exception e) {
LogPrintUtils.eTag(TAG, e, "getIdentifier - " + resName + " " + defType + ": " + packageName);
}
return 0;
}
稍微晚几天,在外地
public class ResourcesUtils {
private AssetManager mAsset = AssetManager.class.newInstance();
private Method addAssetPath = mAsset.getClass().getMethod("addAssetPath", String.class);
private Context context;
private Resources pluginResources;
private String packageName;
public ResourcesUtils() throws InstantiationException, IllegalAccessException, NoSuchMethodException {
}
public void init(Context context, String fileUrl) throws InvocationTargetException, IllegalAccessException {
this.context = context;
pluginResources = new Resources(mAsset, context.getResources().getDisplayMetrics(), context.getResources().getConfiguration());
//TODO 此处需要先判断 fileUrl 是否有文件
addAssetPath.invoke(mAsset, fileUrl);
PackageInfo packageArchiveInfo = context.getPackageManager().getPackageArchiveInfo(
fileUrl,
PackageManager.GET_ACTIVITIES
);
packageName = packageArchiveInfo.packageName;
}
public String getString(String stringName){
int stringId = pluginResources.getIdentifier(stringName, "string", packageName);
return pluginResources.getString(stringId);
}
}
已经实现了 ResourcePluginUtils.java,过几天再发个版本,上次的日历工具类:CalendarUtils.java
我之前写了个工具类,就是通过包名,反射获取资源id,value,这个你要么,要的话我回头整理下发你,你塞里吧