ActivityThread作为应用的主线程,是我们平时android开发中接触最多的一个线程,本文会分析ActivityThread.java这个类里面的一些关键方法和成员变量,通过这些分析,会理清楚,如下问题
- 应用进程是如何和system_server进行通信的
- ActivityThread.java的这个大致的设计思路
分析ActivityThread,我们要关注几个关键词:
- main()
- IApplicationThread
- IActivityManager
- 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