深入理解应用主线程ActivityThread

本文详细探讨了ActivityThread在Android应用中的核心作用,包括其main()函数启动过程,通过IActivityManager和ApplicationThread实现与system_server的通信,以及H()类在处理线程间消息传递的角色。通过分析,揭示了ActivityThread如何维护与system_server的双向通信,并利用Handler机制在不同线程间协调生命周期管理。
摘要由CSDN通过智能技术生成

ActivityThread作为应用的主线程,是我们平时android开发中接触最多的一个线程,本文会分析ActivityThread.java这个类里面的一些关键方法和成员变量,通过这些分析,会理清楚,如下问题

  1. 应用进程是如何和system_server进行通信的
  2. ActivityThread.java的这个大致的设计思路

分析ActivityThread,我们要关注几个关键词:

  1. main()
  2. IApplicationThread
  3. IActivityManager
  4. H()

一、main()

熟悉进程启动流程的都知道,ActivityThrad的main函数是在,进程创建完成后有zytegote最终通过反射调用的

  /**
     * Invokes a static "main(argv[]) method on class "className".
     * Converts various failing exceptions into RuntimeExceptions, with
     * the assumption that they will then cause the VM instance to exit.
     *
     * @param className Fully-qualified class name
     * @param argv Argument vector for main()
     * @param classLoader the classLoader to load {@className} with
     */
    private static Runnable findStaticMain(String className, String[] argv,
            ClassLoader classLoader) {
        Class<?> cl;

        try {
            cl = Class.forName(className, true, classLoader);
        } catch (ClassNotFoundException ex) {
            throw new RuntimeException(
                    "Missing class when invoking static main " + className,
                    ex);
        }

        Method m;
        try {
            m = cl.getMethod("main", new Class[] { String[].class });
        } catch (NoSuchMethodException ex) {
            throw new RuntimeException(
                    "Missing static main on " + className, ex);
        } catch (SecurityException ex) {
            throw new RuntimeException(
                    "Problem getting static main on " + className, ex);
        }

        int modifiers = m.getModifiers();
        if (! (Modifier.isStatic(modifiers) && Modifier.isPublic(modifiers))) {
            throw new RuntimeException
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值