xUtils源码阅读(1)- x

这篇博客主要探讨xUtils框架,从上至下解析其核心功能。内容包括数据库操作、Http请求、图片管理、View注入以及任务调度机制,详细解读了xUtils如何将这些功能封装为任务进行执行。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这次阅读xUtils我们从上到下的方式读。

先看看这个这么另类的类x吧。

很简单,就是对整个项目的一个整体功能的总括。

共包括四部分:

1、数据库

2、Http访问

3、图片管理

4、view注入

5、任务管理(以上各项在执行的时候都要包装成一个个的任务执行)


源码:

/**
 * Created by wyouflf on 15/6/10.
 * 任务控制中心, http, image, db, view注入等接口的入口.
 * 需要在在application的onCreate中初始化: x.Ext.init(this);
 */
public final class x {//静态类,其方法都是静态的。

    private x() {//防止构造对象
    }

    public static boolean isDebug() {//是否要debug
        return Ext.debug;
    }

    public static Application app() {//获取当前的Application
        if (Ext.app == null) {
            try {
                // 在IDE进行布局预览时使用
                Class<?> renderActionClass = Class.forName("com.android.layoutlib.bridge.impl.RenderAction");
                Method method = renderActionClass.getDeclaredMethod("getCurrentContext");
                Context context = (Context) method.invoke(null);
                Ext.app = new MockApplication(context);
            } catch (Throwable ignored) {
                throw new RuntimeException("please invoke x.Ext.init(app) on Application#onCreate()"
                        + " and register your Application in manifest.");
            }
        }
        return Ext.app;
    }

    public static TaskController task() {//所使用的任务管理器
        return Ext.taskController;
    }

    public static HttpManager http() {//所使用的http管理器
        if (Ext.httpManager == null) {
            HttpManagerImpl.registerInstance();
        }
        return Ext.httpManager;
    }

    public static ImageManager image() {//所使用的image管理器
        if (Ext.imageManager == null) {
            ImageManagerImpl.registerInstance();
        }
        return Ext.imageManager;
    }

    public static ViewInjector view() {//所使用的view注入器
        if (Ext.viewInjector == null) {
            ViewInjectorImpl.registerInstance();
        }
        return Ext.viewInjector;
    }

    public static DbManager getDb(DbManager.DaoConfig daoConfig) {//数据库管理器
        return DbManagerImpl.getInstance(daoConfig);
    }

    public static class Ext {//内部类,同时也是配置类,各个关键部件的引用管理类
        private static boolean debug;
        private static Application app;
        private static TaskController taskController;
        private static HttpManager httpManager;
        private static ImageManager imageManager;
        private static ViewInjector viewInjector;

        private Ext() {
        }

        static {
            TaskControllerImpl.registerInstance();
            // 默认信任所有https域名
            HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {
                @Override
                public boolean verify(String hostname, SSLSession session) {
                    return true;
                }
            });
        }

        public static void init(Application app) {
            if (Ext.app == null) {
                Ext.app = app;
            }
        }

        public static void setDebug(boolean debug) {
            Ext.debug = debug;
        }

        public static void setTaskController(TaskController taskController) {
            if (Ext.taskController == null) {
                Ext.taskController = taskController;
            }
        }

        public static void setHttpManager(HttpManager httpManager) {
            Ext.httpManager = httpManager;
        }

        public static void setImageManager(ImageManager imageManager) {
            Ext.imageManager = imageManager;
        }

        public static void setViewInjector(ViewInjector viewInjector) {
            Ext.viewInjector = viewInjector;
        }
    }

    private static class MockApplication extends Application {
        public MockApplication(Context baseContext) {
            this.attachBaseContext(baseContext);//绑定到特定的baseContext
        }
    }
}


xUtils快速使用说明 导入依赖 使用Gradle构建时添加一下依赖即可: javascript compile 'org.xutils:xutils:3.3.22' 如果使用eclipse可以 点击这里下载aar文件, 然后用zip解压, 取出jar包和so文件. 添加配置混淆设置 region for xUtils -keepattributes Signature,Annotation -keep public class org.xutils.** { public protected ; } -keep public interface org.xutils.* { public protected ; } -keepclassmembers class * extends org.xutils.* { public protected ; } -keepclassmembers @org.xutils.db.annotation. class * {;} -keepclassmembers @org.xutils.http.annotation. class * {*;} -keepclassmembers class * { @org.xutils.view.annotation.Event ; } end region 配置权限 需要的权限 xml <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 初始化 如果为初始化会报 RuntimeException: "please invoke x.Ext.init(app) on Application#onCreate() and register your Application in manifest." 在AndroidManifest.xml中注册自定义Application, 之后加入下面的代码. java // 在applicationonCreate中初始化 @Override public void onCreate() { super.onCreate(); x.Ext.init(this); // 这一步之后, 我们就可以在任何地方使用x.app()来获取Application的实例了. x.Ext.setDebug(true); // 是否输出debug日志 ... } 现在可以开始使用xUtils了 常用的API可以参考 README 接下来我们较为详细的介绍没一个模块的api和特性.
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值