afkT / DevUtils

:fire: ( 持续更新,目前含 300+ 工具类 ) DevUtils 是一个 Android 工具库,主要根据不同功能模块,封装快捷使用的工具类及 API 方法调用。该项目尽可能的便于开发人员,快捷、高效开发安全可靠的项目。
Apache License 2.0
1.34k stars 302 forks source link

你没看错,我又来了 #11

Closed JiZhi-Error closed 3 years ago

JiZhi-Error commented 4 years ago

我之前写了个工具类,就是通过包名,反射获取资源id,value,这个你要么,要的话我回头整理下发你,你塞里吧

afkT commented 4 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;
    }
JiZhi-Error commented 3 years ago

稍微晚几天,在外地

JiZhi-Error commented 3 years ago
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);
    }
}
afkT commented 3 years ago

已经实现了 ResourcePluginUtils.java,过几天再发个版本,上次的日历工具类:CalendarUtils.java