D:\linux\linuxkernel\src\cm10.1\frameworks\base\core\java\android\app\ActivityThread.java
/**
* This manages the execution of the main thread in an
* application process, scheduling and executing activities,
* broadcasts, and other operations on it as the activity
* manager requests.
*
* {@hide}
*/
public final class ActivityThread {
final ApplicationThread mAppThread = new ApplicationThread();
http://www.devdiv.com/forum.php?mod=viewthread&tid=38129
但是他们不是线程,你看看继承关系就可以知道ApplicationThread是binder, ActivityThread是一个final类也不是真正的线程。
如果你用ddms查看某一个进程的所有线程,你会发现它只有一个main thread,当然它可能还有binder thread, jdwp, signal catcher等。
一个应用程序对应一个ActivityThread实例,应用程序由activityThread打开消息循环, 同时,一个应用程序也对应一个ApplicationThread对象,此对象是ActivityThread 与 ActivityManagerService连接的桥梁
应该不是一个应用程序对应一个ActivityThread实例,而是一个进程对应一个ActivityThread实例,这个进程里面所有的activity对应这一个ActivityThread实例,你可以看看ActivityThread类包含了mActivities。
http://blog.csdn.net/luoshengyang/article/details/6689748
Step 3. Activity.startActivityForResult
这个函数也是实现在frameworks/base/core/java/android/app/Activity.java文件中:
- public class Activity extends ContextThemeWrapper
- implements LayoutInflater.Factory,
- Window.Callback, KeyEvent.Callback,
- OnCreateContextMenuListener, ComponentCallbacks {
- ......
- public void startActivityForResult(Intent intent, int requestCode) {
- if (mParent == null) {
- Instrumentation.ActivityResult ar =
- mInstrumentation.execStartActivity(
- this, mMainThread.getApplicationThread(), mToken, this,
- intent, requestCode);
- ......
- } else {
- ......
- }
- ......
- }
这里的mMainThread也是Activity类的成员变量,它的类型是ActivityThread,它代表的是应用程序的主线程,我们在Android系统在新进程中启动自定义服务过程(startService)的原理分析一文中已经介绍过了。这里通过mMainThread.getApplicationThread获得它里面的ApplicationThread成员变量,它是一个Binder对象,后面我们会看到,ActivityManagerService会使用它来和ActivityThread来进行进程间通信。这里我们需注意的是,这里的mMainThread代表的是Launcher应用程序运行的进程。
这里的mToken也是Activity类的成员变量,它是一个Binder对象的远程接口。
http://dengzhangtao.iteye.com/blog/1779922
以前一直都说Activity的人口是onCreate方法。其实android上一个应用的入口,应该是ActivityThread。和普通的java类一样,入口是一个main方法。
public static final void main(String[] args) {
SamplingProfilerIntegration.start();
……
Looper.prepareMainLooper();
if (sMainThreadHandler == null) {
sMainThreadHandler = new Handler();
}
ActivityThread thread = new ActivityThread();
thread.attach(false);
……
Looper.loop();
……
thread.detach();
……
Slog.i(TAG, "Main thread of " + name + " is now exiting");
}
下面仔细分析一下这个main方法。
2.Looper.prepareMainLooper();
ActivityThread其实就是我们经常说的UI thread,也就是主线程。我们都知道主线程可以使用Handler进行异步通信,因为主线程中已经创建了Looper,而这个Looper就是在这里创建的。如果其他线程需要使用Handler通信,就要自己去创建Looper。
3. sMainThreadHandler = new Handler();
创建一个Handler。
4. ActivityThread thread = new ActivityThread();
创建ActivityThread 对象。
ActivityThread 有几个比较重要的成员变量,会在创建ActivityThread对象时初始化。
(1)final ApplicationThread mAppThread = new ApplicationThread();
ApplicationThread继承自ApplicationThreadNative, 而ApplicationThreadNative又继承自Binder并实现了IApplicationThread接口。IApplicationThread继承自IInterface。这是一个很明显的binder结构,用于于Ams通信。IApplicationThread接口定义了对一个程序(linux的进程)操作的接口。ApplicationThread通过binder与Ams通信,并将Ams的调用,通过下面的H类(也就是Hnalder)将消息发送到消息队列,然后进行相应的操作,入activity的start, stop。
(2)final H mH = new H();
private final class H extends Handler
mH负责处理ApplicationThread发送到消息队列的消息,例如:
public void handleMessage(Message msg) {
if (DEBUG_MESSAGES) Slog.v(TAG, ">>> handling: " + msg.what);
switch (msg.what) {
case LAUNCH_ACTIVITY: {
ActivityClientRecord r = (ActivityClientRecord)msg.obj;
r.packageInfo = getPackageInfoNoCheck(
r.activityInfo.applicationInfo);
handleLaunchActivity(r, null);
} break;
5. handleLaunchActivity(r, null);
从名字中就可以看出,这里就将进行启动activity的工作了。
方法中主要调用了这一句:
Activity a = performLaunchActivity(r, customIntent);
6. performLaunchActivity()
进行了一些初始化和赋值操作后,创建activity。
activity = mInstrumentation.newActivity(
cl, component.getClassName(), r.intent);
然后调用:
mInstrumentation.callActivityOnCreate(activity, r.state);
这一句就会调用到acitivity的onCreate方法了,就进入了大多数应用开发的入口了。