Android源码分析(三)-----系统框架设计思想

一 : 术在内而道在外

Android系统的精髓在源码之外,而不在源码之内,代码只是一种实现人类思想的工具,仅此而已......

近来发现很多关于Android文章都是以源码的方向入手分析Android这个系统,最终结果可想而知,甚是凄惨。正所谓自信满满的来,一脸懵逼的走。
想要真正了解Android系统,首先一定要站在更高的层次上做分析,以设计者的角度去思考。如果让你去设计Android系统,你会如何设计?而并不是把已经设计好的源码当做学习Android系统的切入点,源码只是让你进一步去验证自己的想法是否正确。Android Framework设计之初就必须要考虑系统诞生后,AP开发者该如何使用,怎么设计系统才会处于主导地位,占据主动权。

二 : 框架设计

框架是如何知道开发者后来撰写的应用子类的名称呢? 如果不知道应用子类的名称又如何创建应用子类的对象呢?
答案是:依赖AndroidManifest.xml文档。 java中的反射机制还有泛型。
image.png

任何系统的设计都是框架在先,AP开发者在后。Framework层框架的设计同样是早于AP开发者,所有的设计基本都是反向调用,基于IOC(控制反转),类似Spring框架的IOC/DI(控制反转/依赖注入),包括Activity的生命周期,setContentView(int layoutResId),startActivity(Intent , intent)等方法全部都是在Framework诞生时就已经定义完成。

源码路径:/frameworks/base/core/java/android/app/Activity.java

    /**
     * Set the activity content from a layout resource.  The resource will be
     * inflated, adding all top-level views to the activity.
     *
     * @param layoutResID Resource ID to be inflated.
     *
     * @see #setContentView(android.view.View)
     * @see #setContentView(android.view.View, android.view.ViewGroup.LayoutParams)
     */
    public void setContentView(@LayoutRes int layoutResID) {
        getWindow().setContentView(layoutResID);
        initWindowDecorActionBar();
    }

    /**
     * Set the activity content to an explicit view.  This view is placed
     * directly into the activity's view hierarchy.  It can itself be a complex
     * view hierarchy.  When calling this method, the layout parameters of the
     * specified view are ignored.  Both the width and the height of the view are
     * set by default to {@link ViewGroup.LayoutParams#MATCH_PARENT}. To use
     * your own layout parameters, invoke
     * {@link #setContentView(android.view.View, android.view.ViewGroup.LayoutParams)}
     * instead.
     *
     * @param view The desired content to display.
     *
     * @see #setContentView(int)
     * @see #setContentView(android.view.View, android.view.ViewGroup.LayoutParams)
     */
    public void setContentView(View view) {
        getWindow().setContentView(view);
        initWindowDecorActionBar();
    }

    /**
     * Set the activity content to an explicit view.  This view is placed
     * directly into the activity's view hierarchy.  It can itself be a complex
     * view hierarchy.
     *
     * @param view The desired content to display.
     * @param params Layout parameters for the view.
     *
     * @see #setContentView(android.view.View)
     * @see #setContentView(int)
     */
    public void setContentView(View view, ViewGroup.LayoutParams params) {
        getWindow().setContentView(view, params);
        initWindowDecorActionBar();
    }

Android中的对象由基类创建,基类主导一切的控制权,这样框架便占据了主导地位。在RunTime时刻根据Manifest文件回传相关数据。AP开发者同样也可以使用反射机制调用Framework的隐藏API。
image.png

三 : 总结

  1. Android的精髓在源码之外,而不在源码之内。
  2. 以设计者的角度去思考系统架构。
  3. 从源码中验证自己的结论。
  4. 多角度思考,重复验证。

喜欢源码分析系列可参考其他文章:
Android源码分析(一)-----如何快速掌握Android编译文件
Android源码分析(二)-----如何编译修改后的framework资源文件

猜你喜欢

转载自www.cnblogs.com/ljx646566715/p/10893856.html